rm(list=ls())

#set working directory -- needs to be updated locally:
setwd("~/Dropbox/Corruption and Political Participation/Corruption and Political Participation/PSRM/Replication Files")

library(foreign)
library(stargazer)
library(ggplot2)
library(gridExtra)

##read in Latinobarometro data:
full <- read.dta("Latinobarometro2009Eng.dta")

COR <- subset(full,idenpa=="Costa Rica")

COR$date <- as.numeric(gsub("[-]", "" , COR$date, perl=TRUE))

##treatment (before vs. after Oct. 5, 2009)
COR$treat14 <- ifelse(COR$date < 20091005,0,NA)
COR$treat14 <- ifelse(COR$date > 20091005 & COR$date<20091020,1,COR$treat14)

##recode variables for analysis:
#gender:
COR$male <- ifelse(COR$s5=="Male",1,0)

#age:
COR$age <- COR$s6

#level of education:
COR$incomplete_highschool <- ifelse(COR$reeduc1=="Illiterate"|COR$reeduc1=="Incomplete primary"|COR$reeduc1=="Complete primary"|COR$reeduc1=="Incomplete Secondary, technical",1,0)
COR$complete_highschool <- ifelse(COR$reeduc1=="Complete Secondary, technical",1,0)
COR$university <- ifelse(COR$reeduc1=="Incomplete high"|COR$reeduc1=="Complete high",1,0)

#part of the labor force:
COR$laborforce <- ifelse(COR$s14a=="Self employed"|COR$s14a=="Salaried employee in a public company"|COR$s14a=="Salaried employee in a private company"|COR$s14a=="Temporarily out of work",1,0)

#poverty:
COR$poverty <- ifelse(COR$s4b=="No",0,1)
COR$poverty <- ifelse(COR$s4b=="Don't know",NA,COR$poverty)
COR$poverty <- ifelse(COR$s4b=="No answer/Refused",NA,COR$poverty)

#voted in last election:
COR$voted <- ifelse(COR$p37stm=="I voted in last election",1,0)
COR$voted <- ifelse(COR$p37stm=="Don't remember what I did/don't know"|COR$p37stm=="No answer",NA,COR$voted)

#progress on corruption made:
COR$progcorrupt <- ifelse(COR$p74st=="No progress at all",0,NA)
COR$progcorrupt <- ifelse(COR$p74st=="Little",1,COR$progcorrupt)
COR$progcorrupt <- ifelse(COR$p74st=="Some",2,COR$progcorrupt)
COR$progcorrupt <- ifelse(COR$p74st=="Much",3,COR$progcorrupt)

#can state solve corruption
#solvecorrupt
#question not asked in COR

#extent of local corruption
#localcorrupt
#question not asked in COR

#extent of national corruption
#nationalcorrupt
#question not asked in COR

#inclination to vote or protest:
COR$participation_willingness <- ifelse(COR$p28st=="It is not possible to contribute to change things", 0, 1)
COR$participation_willingness <- ifelse(COR$p28st=="No answer/Refused" | COR$p28st=="None of the above", NA, COR$participation_willingness)

#null vote (voto nulo:)
COR$vote_null <- ifelse(COR$p35st=="Null vote/Blank vote",1,0)
COR$vote_null <- ifelse(COR$p35st=="No answer"|COR$p35st=="Not registered/No legal age",NA,COR$vote_null)

#not voting (staying at home):
COR$vote_not <- ifelse(COR$p35st=="Do not vote/None",1,0)
COR$vote_not <- ifelse(COR$p35st=="No answer"|COR$p35st=="Not registered/No legal age",NA,COR$vote_not)

#invalid vote (either null vote or not voting):
COR$vote_invalid <- ifelse(COR$vote_null==1 | COR$vote_not==1, 1, 0)

#trust in the judiciary:
COR$conf_judge <- ifelse(COR$p26st_b=="A lot of confidence",3,NA)
COR$conf_judge <- ifelse(COR$p26st_b=="Some confidence",2,COR$conf_judge)
COR$conf_judge <- ifelse(COR$p26st_b=="Little confidence",1,COR$conf_judge)
COR$conf_judge <- ifelse(COR$p26st_b=="No confidence at all",0,COR$conf_judge)

#trust in the political parties:
COR$conf_parties <- ifelse(COR$p26st_c=="A lot of confidence",3,NA)
COR$conf_parties <- ifelse(COR$p26st_c=="Some confidence",2,COR$conf_parties)
COR$conf_parties <- ifelse(COR$p26st_c=="Little confidence",1,COR$conf_parties)
COR$conf_parties <- ifelse(COR$p26st_c=="No confidence at all",0,COR$conf_parties)

#trust in the legislature:
COR$conf_parl <- ifelse(COR$p26st_a=="A lot of confidence",3,NA)
COR$conf_parl <- ifelse(COR$p26st_a=="Some confidence",2,COR$conf_parl)
COR$conf_parl <- ifelse(COR$p26st_a=="Little confidence",1,COR$conf_parl)
COR$conf_parl <- ifelse(COR$p26st_a=="No confidence at all",0,COR$conf_parl)

#trust in the church:
COR$conf_church <- ifelse(COR$p26st_g=="A lot of confidence",3,NA)
COR$conf_church <- ifelse(COR$p26st_g=="Some confidence",2,COR$conf_church)
COR$conf_church <- ifelse(COR$p26st_g=="Little confidence",1,COR$conf_church)
COR$conf_church <- ifelse(COR$p26st_g=="No confidence at all",0,COR$conf_church)

#trust in the local government:
COR$conf_local_gov <- ifelse(COR$p26st_f=="A lot",3,NA)
COR$conf_local_gov <- ifelse(COR$p26st_f=="Some",2,COR$conf_local_gov)
COR$conf_local_gov <- ifelse(COR$p26st_f=="Little",1,COR$conf_local_gov)
COR$conf_local_gov <- ifelse(COR$p26st_f=="None",0,COR$conf_local_gov)


##number of observations in cities with observations on both sides of threshold:

band14 <- subset(COR,!is.na(COR$treat14))
cities14T <- unique(subset(band14, band14$treat14==1)$ciudad) #cities in treatment
cities14C <- unique(subset(band14, band14$treat14==0)$ciudad) #cities in control
cities14 <- intersect(cities14T,cities14C) #cities with observations in both treatment and control
band14 <- subset(band14, band14$ciudad %in% cities14) #observations from cities that are in both treatment and control
nrow(band14)

nrow(subset(band14,band14$treat14==1)) #observations in treatment
nrow(subset(band14,band14$treat14==0)) #observations in control

#observations with all covariates:

band14_controls <- subset(band14,(!is.na(band14$male) & !is.na(band14$age) & !is.na(band14$incomplete_highschool) & !is.na(band14$complete_highschool) & !is.na(band14$university) & !is.na(band14$laborforce) & !is.na(band14$poverty) & !is.na(band14$voted)))
cities14_controls_T <- unique(subset(band14_controls, band14_controls$treat14==1)$ciudad) #cities in treatment
cities14_controls_C <- unique(subset(band14_controls, band14_controls$treat14==0)$ciudad) #cities in control
cities14_controls <- intersect(cities14_controls_T,cities14_controls_C) #cities with observations in both treatment and control
band14_controls <- subset(band14_controls, band14_controls$ciudad %in% cities14_controls) #observations from cities that are in both treatment and control
nrow(band14_controls)

nrow(subset(band14_controls,band14_controls$treat14==1)) #observations in treatment
nrow(subset(band14_controls,band14_controls$treat14==0)) #observations in control


#valid observations for each covariate:

#age:
band14_controls_age <- subset(band14,!is.na(band14$age))
cities14_controls_T_age <- unique(subset(band14_controls_age, band14_controls_age$treat14==1)$ciudad) #cities in treatment
cities14_controls_C_age <- unique(subset(band14_controls_age, band14_controls_age$treat14==0)$ciudad) #cities in control
cities14_controls_age <- intersect(cities14_controls_T_age,cities14_controls_C_age) #cities with observations in both treatment and control
band14_controls_age <- subset(band14_controls_age, band14_controls_age$ciudad %in% cities14_controls_age) #observations from cities that are in both treatment and control
nrow(band14_controls_age)

#male:
band14_controls_male <- subset(band14,!is.na(band14$male))
cities14_controls_T_male <- unique(subset(band14_controls_male, band14_controls_male$treat14==1)$ciudad) #cities in treatment
cities14_controls_C_male <- unique(subset(band14_controls_male, band14_controls_male$treat14==0)$ciudad) #cities in control
cities14_controls_male <- intersect(cities14_controls_T_male,cities14_controls_C_male) #cities with observations in both treatment and control
band14_controls_male <- subset(band14_controls_male, band14_controls_male$ciudad %in% cities14_controls_male) #observations from cities that are in both treatment and control
nrow(band14_controls_male)

#incomplete_highschool:
band14_controls_incomplete_highschool <- subset(band14,!is.na(band14$incomplete_highschool))
cities14_controls_T_incomplete_highschool <- unique(subset(band14_controls_incomplete_highschool, band14_controls_incomplete_highschool$treat14==1)$ciudad) #cities in treatment
cities14_controls_C_incomplete_highschool <- unique(subset(band14_controls_incomplete_highschool, band14_controls_incomplete_highschool$treat14==0)$ciudad) #cities in control
cities14_controls_incomplete_highschool <- intersect(cities14_controls_T_incomplete_highschool,cities14_controls_C_incomplete_highschool) #cities with observations in both treatment and control
band14_controls_incomplete_highschool <- subset(band14_controls_incomplete_highschool, band14_controls_incomplete_highschool$ciudad %in% cities14_controls_incomplete_highschool) #observations from cities that are in both treatment and control
nrow(band14_controls_incomplete_highschool)

#complete_highschool:
band14_controls_complete_highschool <- subset(band14,!is.na(band14$complete_highschool))
cities14_controls_T_complete_highschool <- unique(subset(band14_controls_complete_highschool, band14_controls_complete_highschool$treat14==1)$ciudad) #cities in treatment
cities14_controls_C_complete_highschool <- unique(subset(band14_controls_complete_highschool, band14_controls_complete_highschool$treat14==0)$ciudad) #cities in control
cities14_controls_complete_highschool <- intersect(cities14_controls_T_complete_highschool,cities14_controls_C_complete_highschool) #cities with observations in both treatment and control
band14_controls_complete_highschool <- subset(band14_controls_complete_highschool, band14_controls_complete_highschool$ciudad %in% cities14_controls_complete_highschool) #observations from cities that are in both treatment and control
nrow(band14_controls_complete_highschool)

#university:
band14_controls_university <- subset(band14,!is.na(band14$university))
cities14_controls_T_university <- unique(subset(band14_controls_university, band14_controls_university$treat14==1)$ciudad) #cities in treatment
cities14_controls_C_university <- unique(subset(band14_controls_university, band14_controls_university$treat14==0)$ciudad) #cities in control
cities14_controls_university <- intersect(cities14_controls_T_university,cities14_controls_C_university) #cities with observations in both treatment and control
band14_controls_university <- subset(band14_controls_university, band14_controls_university$ciudad %in% cities14_controls_university) #observations from cities that are in both treatment and control
nrow(band14_controls_university)

#laborforce:
band14_controls_laborforce <- subset(band14,!is.na(band14$laborforce))
cities14_controls_T_laborforce <- unique(subset(band14_controls_laborforce, band14_controls_laborforce$treat14==1)$ciudad) #cities in treatment
cities14_controls_C_laborforce <- unique(subset(band14_controls_laborforce, band14_controls_laborforce$treat14==0)$ciudad) #cities in control
cities14_controls_laborforce <- intersect(cities14_controls_T_laborforce,cities14_controls_C_laborforce) #cities with observations in both treatment and control
band14_controls_laborforce <- subset(band14_controls_laborforce, band14_controls_laborforce$ciudad %in% cities14_controls_laborforce) #observations from cities that are in both treatment and control
nrow(band14_controls_laborforce)

#poverty:
band14_controls_poverty <- subset(band14,!is.na(band14$poverty))
cities14_controls_T_poverty <- unique(subset(band14_controls_poverty, band14_controls_poverty$treat14==1)$ciudad) #cities in treatment
cities14_controls_C_poverty <- unique(subset(band14_controls_poverty, band14_controls_poverty$treat14==0)$ciudad) #cities in control
cities14_controls_poverty <- intersect(cities14_controls_T_poverty,cities14_controls_C_poverty) #cities with observations in both treatment and control
band14_controls_poverty <- subset(band14_controls_poverty, band14_controls_poverty$ciudad %in% cities14_controls_poverty) #observations from cities that are in both treatment and control
nrow(band14_controls_poverty)

#voted:
band14_controls_voted <- subset(band14,!is.na(band14$voted))
cities14_controls_T_voted <- unique(subset(band14_controls_voted, band14_controls_voted$treat14==1)$ciudad) #cities in treatment
cities14_controls_C_voted <- unique(subset(band14_controls_voted, band14_controls_voted$treat14==0)$ciudad) #cities in control
cities14_controls_voted <- intersect(cities14_controls_T_voted,cities14_controls_C_voted) #cities with observations in both treatment and control
band14_controls_voted <- subset(band14_controls_voted, band14_controls_voted$ciudad %in% cities14_controls_voted) #observations from cities that are in both treatment and control
nrow(band14_controls_voted)


##valid observations for each outcome:

#progcorrupt:
band14_controls_progcorrupt <- subset(band14,!is.na(band14$progcorrupt))
cities14_controls_T_progcorrupt <- unique(subset(band14_controls_progcorrupt, band14_controls_progcorrupt$treat14==1)$ciudad) #cities in treatment
cities14_controls_C_progcorrupt <- unique(subset(band14_controls_progcorrupt, band14_controls_progcorrupt$treat14==0)$ciudad) #cities in control
cities14_controls_progcorrupt <- intersect(cities14_controls_T_progcorrupt,cities14_controls_C_progcorrupt) #cities with observations in both treatment and control
band14_controls_progcorrupt <- subset(band14_controls_progcorrupt, band14_controls_progcorrupt$ciudad %in% cities14_controls_progcorrupt) #observations from cities that are in both treatment and control
nrow(band14_controls_progcorrupt)

#participation_willingness:
band14_controls_participation_willingness <- subset(band14,!is.na(band14$participation_willingness))
cities14_controls_T_participation_willingness <- unique(subset(band14_controls_participation_willingness, band14_controls_participation_willingness$treat14==1)$ciudad) #cities in treatment
cities14_controls_C_participation_willingness <- unique(subset(band14_controls_participation_willingness, band14_controls_participation_willingness$treat14==0)$ciudad) #cities in control
cities14_controls_participation_willingness <- intersect(cities14_controls_T_participation_willingness,cities14_controls_C_participation_willingness) #cities with observations in both treatment and control
band14_controls_participation_willingness <- subset(band14_controls_participation_willingness, band14_controls_participation_willingness$ciudad %in% cities14_controls_participation_willingness) #observations from cities that are in both treatment and control
nrow(band14_controls_participation_willingness)

#vote_invalid:
band14_controls_vote_invalid <- subset(band14,!is.na(band14$vote_invalid))
cities14_controls_T_vote_invalid <- unique(subset(band14_controls_vote_invalid, band14_controls_vote_invalid$treat14==1)$ciudad) #cities in treatment
cities14_controls_C_vote_invalid <- unique(subset(band14_controls_vote_invalid, band14_controls_vote_invalid$treat14==0)$ciudad) #cities in control
cities14_controls_vote_invalid <- intersect(cities14_controls_T_vote_invalid,cities14_controls_C_vote_invalid) #cities with observations in both treatment and control
band14_controls_vote_invalid <- subset(band14_controls_vote_invalid, band14_controls_vote_invalid$ciudad %in% cities14_controls_vote_invalid) #observations from cities that are in both treatment and control
nrow(band14_controls_vote_invalid)

#conf_judge:
band14_controls_conf_judge <- subset(band14,!is.na(band14$conf_judge))
cities14_controls_T_conf_judge <- unique(subset(band14_controls_conf_judge, band14_controls_conf_judge$treat14==1)$ciudad) #cities in treatment
cities14_controls_C_conf_judge <- unique(subset(band14_controls_conf_judge, band14_controls_conf_judge$treat14==0)$ciudad) #cities in control
cities14_controls_conf_judge <- intersect(cities14_controls_T_conf_judge,cities14_controls_C_conf_judge) #cities with observations in both treatment and control
band14_controls_conf_judge <- subset(band14_controls_conf_judge, band14_controls_conf_judge$ciudad %in% cities14_controls_conf_judge) #observations from cities that are in both treatment and control
nrow(band14_controls_conf_judge)

#conf_parties:
band14_controls_conf_parties <- subset(band14,!is.na(band14$conf_parties))
cities14_controls_T_conf_parties <- unique(subset(band14_controls_conf_parties, band14_controls_conf_parties$treat14==1)$ciudad) #cities in treatment
cities14_controls_C_conf_parties <- unique(subset(band14_controls_conf_parties, band14_controls_conf_parties$treat14==0)$ciudad) #cities in control
cities14_controls_conf_parties <- intersect(cities14_controls_T_conf_parties,cities14_controls_C_conf_parties) #cities with observations in both treatment and control
band14_controls_conf_parties <- subset(band14_controls_conf_parties, band14_controls_conf_parties$ciudad %in% cities14_controls_conf_parties) #observations from cities that are in both treatment and control
nrow(band14_controls_conf_parties)

#conf_parl:
band14_controls_conf_parl <- subset(band14,!is.na(band14$conf_parl))
cities14_controls_T_conf_parl <- unique(subset(band14_controls_conf_parl, band14_controls_conf_parl$treat14==1)$ciudad) #cities in treatment
cities14_controls_C_conf_parl <- unique(subset(band14_controls_conf_parl, band14_controls_conf_parl$treat14==0)$ciudad) #cities in control
cities14_controls_conf_parl <- intersect(cities14_controls_T_conf_parl,cities14_controls_C_conf_parl) #cities with observations in both treatment and control
band14_controls_conf_parl <- subset(band14_controls_conf_parl, band14_controls_conf_parl$ciudad %in% cities14_controls_conf_parl) #observations from cities that are in both treatment and control
nrow(band14_controls_conf_parl)


##valid observations with data for all covariates and outcomes:
band14_controls_full <- subset(band14,!is.na(age) & !is.na(male) & !is.na(incomplete_highschool) & !is.na(complete_highschool) & !is.na(university) & !is.na(laborforce) & !is.na(poverty) & !is.na(voted) & !is.na(progcorrupt) & !is.na(participation_willingness) & !is.na(vote_invalid) & !is.na(conf_judge) & !is.na(conf_parties) & !is.na(conf_parl))
cities14_controls_T_full <- unique(subset(band14_controls_full, band14_controls_full$treat14==1)$ciudad) #cities in treatment
cities14_controls_C_full <- unique(subset(band14_controls_full, band14_controls_full$treat14==0)$ciudad) #cities in control
cities14_controls_conf_parl <- intersect(cities14_controls_T_full,cities14_controls_C_full) #cities with observations in both treatment and control
band14_controls_full <- subset(band14_controls_full, band14_controls_full$ciudad %in% cities14_controls_conf_parl) #observations from cities that are in both treatment and control
nrow(band14_controls_full)

nrow(subset(band14_controls_full,band14_controls_full$treat14==1)) #observations in treatment
nrow(subset(band14_controls_full,band14_controls_full$treat14==0)) #observations in control


##valid observations with data for all outcomes (but not covariates):
band14_controls_full <- subset(band14, !is.na(progcorrupt) & !is.na(participation_willingness) & !is.na(vote_invalid) & !is.na(conf_judge) & !is.na(conf_parties) & !is.na(conf_parl))
cities14_controls_T_full <- unique(subset(band14_controls_full, band14_controls_full$treat14==1)$ciudad) #cities in treatment
cities14_controls_C_full <- unique(subset(band14_controls_full, band14_controls_full$treat14==0)$ciudad) #cities in control
cities14_controls_conf_parl <- intersect(cities14_controls_T_full,cities14_controls_C_full) #cities with observations in both treatment and control
band14_controls_full <- subset(band14_controls_full, band14_controls_full$ciudad %in% cities14_controls_conf_parl) #observations from cities that are in both treatment and control
nrow(band14_controls_full)

nrow(subset(band14_controls_full,band14_controls_full$treat14==1)) #observations in treatment
nrow(subset(band14_controls_full,band14_controls_full$treat14==0)) #observations in control


##balance tests:
##Table A6:

balance <- 
  rbind(
    c("age",
      summary(lm(age ~ treat14 + factor(ciudad),data=COR))$coefficients[1,1],
      summary(lm(age ~ treat14 + factor(ciudad),data=COR))$coefficients[2,][c(1,2,4)],
      sum(ifelse(!is.na(subset(COR,COR$treat14==0|COR$treat14==1)$age),1,0)),
      nrow(band14_controls_age)),
    c("male",
      summary(lm(male ~ treat14 + factor(ciudad),data=COR))$coefficients[1,1],
      summary(lm(male ~ treat14 + factor(ciudad),data=COR))$coefficients[2,][c(1,2,4)],
      sum(ifelse(!is.na(subset(COR,COR$treat14==0|COR$treat14==1)$male),1,0)),
      nrow(band14_controls_male)),
    c("incomplete_highschool",
      summary(lm(incomplete_highschool ~ treat14 + factor(ciudad),data=COR))$coefficients[1,1],
      summary(lm(incomplete_highschool ~ treat14 + factor(ciudad),data=COR))$coefficients[2,][c(1,2,4)],
      sum(ifelse(!is.na(subset(COR,COR$treat14==0|COR$treat14==1)$incomplete_highschool),1,0)),
      nrow(band14_controls_incomplete_highschool)),
    c("complete_highschool",
      summary(lm(complete_highschool ~ treat14 + factor(ciudad),data=COR))$coefficients[1,1],
      summary(lm(complete_highschool ~ treat14 + factor(ciudad),data=COR))$coefficients[2,][c(1,2,4)],
      sum(ifelse(!is.na(subset(COR,COR$treat14==0|COR$treat14==1)$complete_highschool),1,0)),
      nrow(band14_controls_complete_highschool)),
    c("university",
      summary(lm(university ~ treat14 + factor(ciudad),data=COR))$coefficients[1,1],
      summary(lm(university ~ treat14 + factor(ciudad),data=COR))$coefficients[2,][c(1,2,4)],
      sum(ifelse(!is.na(subset(COR,COR$treat14==0|COR$treat14==1)$university),1,0)),
      nrow(band14_controls_university)),
    c("laborforce",
      summary(lm(laborforce ~ treat14 + factor(ciudad),data=COR))$coefficients[1,1],
      summary(lm(laborforce ~ treat14 + factor(ciudad),data=COR))$coefficients[2,][c(1,2,4)],
      sum(ifelse(!is.na(subset(COR,COR$treat14==0|COR$treat14==1)$laborforce),1,0)),
      nrow(band14_controls_laborforce)),
    c("poverty",
      summary(lm(poverty ~ treat14 + factor(ciudad),data=COR))$coefficients[1,1],
      summary(lm(poverty ~ treat14 + factor(ciudad),data=COR))$coefficients[2,][c(1,2,4)],
      sum(ifelse(!is.na(subset(COR,COR$treat14==0|COR$treat14==1)$poverty),1,0)),
      nrow(band14_controls_poverty)),
    c("voted",
      summary(lm(voted ~ treat14 + factor(ciudad),data=COR))$coefficients[1,1],
      summary(lm(voted ~ treat14 + factor(ciudad),data=COR))$coefficients[2,][c(1,2,4)],
      sum(ifelse(!is.na(subset(COR,COR$treat14==0|COR$treat14==1)$voted),1,0)),
      nrow(band14_controls_voted))
  )
balance <- cbind(balance[,c(1:2)],rowSums(cbind(as.numeric(balance[,2]),as.numeric(balance[,3]))),balance[,c(3:7)])

colnames(balance) <- c("Variable","Control","Treatment","Diff. means","SE", "p-value", "Valid N","Analytic N")
balance[,2] <- round(as.numeric(balance[,2]),digits=3)
balance[,3] <- round(as.numeric(balance[,3]),digits=3)
balance[,4] <- round(as.numeric(balance[,4]),digits=3)
balance[,5] <- round(as.numeric(balance[,5]),digits=3)
balance[,6] <- round(as.numeric(balance[,6]),digits=3)

balance.table <- balance[,c(1:4,6:8)]

#Table A6:
stargazer(balance.table, out="TableA6.tex")


###descriptive statistics for outcomes:

##for all observations:
##Table A3:

descriptives <- rbind(
  c("progcorrupt",min(COR$progcorrupt,na.rm=T),max(COR$progcorrupt,na.rm=T),mean(COR$progcorrupt,na.rm=T),median(COR$progcorrupt,na.rm=T),sum(!is.na(COR$progcorrupt))),
  c("participation_willingness",min(COR$participation_willingness,na.rm=T),max(COR$participation_willingness,na.rm=T),mean(COR$participation_willingness,na.rm=T),median(COR$participation_willingness,na.rm=T),sum(!is.na(COR$participation_willingness))),
  c("vote_invalid",min(COR$vote_invalid,na.rm=T),max(COR$vote_invalid,na.rm=T),mean(COR$vote_invalid,na.rm=T),median(COR$vote_invalid,na.rm=T),sum(!is.na(COR$vote_invalid))),
  c("conf_judge",min(COR$conf_judge,na.rm=T),max(COR$conf_judge,na.rm=T),mean(COR$conf_judge,na.rm=T),median(COR$conf_judge,na.rm=T),sum(!is.na(COR$conf_judge))),
  c("conf_parties",min(COR$conf_parties,na.rm=T),max(COR$conf_parties,na.rm=T),mean(COR$conf_parties,na.rm=T),median(COR$conf_parties,na.rm=T),sum(!is.na(COR$conf_parties))),
  c("conf_parl",min(COR$conf_parl,na.rm=T),max(COR$conf_parl,na.rm=T),mean(COR$conf_parl,na.rm=T),median(COR$conf_parl,na.rm=T),sum(!is.na(COR$conf_parl)))
)

colnames(descriptives) <- c("Variable","Minimum","Maximum","Mean","Median","Valid N")
descriptives[,4] <- round(as.numeric(descriptives[,4]),3)
descriptives[,5] <- round(as.numeric(descriptives[,5]),3)
descriptives

#Table A3:
stargazer(descriptives, out="TableA3.tex")


##for analytic sample:
##Table A4:
descriptives_analytic <- rbind(
  c("progcorrupt",min(band14_controls_progcorrupt$progcorrupt,na.rm=T),max(band14_controls_progcorrupt$progcorrupt,na.rm=T),mean(band14_controls_progcorrupt$progcorrupt,na.rm=T),median(band14_controls_progcorrupt$progcorrupt,na.rm=T),sum(!is.na(band14_controls_progcorrupt$progcorrupt))),
  c("participation_willingness",min(band14_controls_participation_willingness$participation_willingness,na.rm=T),max(band14_controls_participation_willingness$participation_willingness,na.rm=T),mean(band14_controls_participation_willingness$participation_willingness,na.rm=T),median(band14_controls_participation_willingness$participation_willingness,na.rm=T),sum(!is.na(band14_controls_participation_willingness$participation_willingness))),
  c("vote_invalid",min(band14_controls_vote_invalid$vote_invalid,na.rm=T),max(band14_controls_vote_invalid$vote_invalid,na.rm=T),mean(band14_controls_vote_invalid$vote_invalid,na.rm=T),median(band14_controls_vote_invalid$vote_invalid,na.rm=T),sum(!is.na(band14_controls_vote_invalid$vote_invalid))),
  c("conf_judge",min(band14_controls_conf_judge$conf_judge,na.rm=T),max(band14_controls_conf_judge$conf_judge,na.rm=T),mean(band14_controls_conf_judge$conf_judge,na.rm=T),median(band14_controls_conf_judge$conf_judge,na.rm=T),sum(!is.na(band14_controls_conf_judge$conf_judge))),
  c("conf_parties",min(band14_controls_conf_parties$conf_parties,na.rm=T),max(band14_controls_conf_parties$conf_parties,na.rm=T),mean(band14_controls_conf_parties$conf_parties,na.rm=T),median(band14_controls_conf_parties$conf_parties,na.rm=T),sum(!is.na(band14_controls_conf_parties$conf_parties))),
  c("conf_parl",min(band14_controls_conf_parl$conf_parl,na.rm=T),max(band14_controls_conf_parl$conf_parl,na.rm=T),mean(band14_controls_conf_parl$conf_parl,na.rm=T),median(band14_controls_conf_parl$conf_parl,na.rm=T),sum(!is.na(band14_controls_conf_parl$conf_parl)))
)

colnames(descriptives_analytic) <- c("Variable","Minimum","Maximum","Mean","Median","Valid N")
descriptives_analytic[,4] <- round(as.numeric(descriptives_analytic[,4]),3)
descriptives_analytic[,5] <- round(as.numeric(descriptives_analytic[,5]),3)

descriptives_analytic

#Table A4:
stargazer(descriptives_analytic, out="TableA4.tex")


##treatment outcomes (with covariates):
##Table A9:
outcomes_controls <- rbind(
  c("progcorrupt",
    summary(lm(progcorrupt ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[1,1],
    summary(lm(progcorrupt ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR,COR$treat14==0|COR$treat14==1)$progcorrupt),1,0))),
  c("participation_willingness",
    summary(lm(participation_willingness ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[1,1],
    summary(lm(participation_willingness ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR,COR$treat14==0|COR$treat14==1)$participation_willingness),1,0))),
  c("vote_invalid",
    summary(lm(vote_invalid ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[1,1],
    summary(lm(vote_invalid ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR,COR$treat14==0|COR$treat14==1)$vote_invalid),1,0))),
  c("conf_judge",
    summary(lm(conf_judge ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[1,1],
    summary(lm(conf_judge ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR,COR$treat14==0|COR$treat14==1)$conf_judge),1,0))),
  c("conf_parties",
    summary(lm(conf_parties ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[1,1],
    summary(lm(conf_parties ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR,COR$treat14==0|COR$treat14==1)$conf_parties),1,0))),
  c("conf_parl",
    summary(lm(conf_parl ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[1,1],
    summary(lm(conf_parl ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR,COR$treat14==0|COR$treat14==1)$conf_parl),1,0)))
)

colnames(outcomes_controls) <- c("Variable",
                                 "Intercept14","Estimate14","SE14","pvalue14","ValidN14")

outcomes_controls

table_appendix <- outcomes_controls[,c(1,3:6)]
table_appendix[,2] <- round(as.numeric(table_appendix[,2]),3)
table_appendix[,3] <- round(as.numeric(table_appendix[,3]),3)
table_appendix[,4] <- round(as.numeric(table_appendix[,4]),3) 

table_appendix[,1] <- c("Progress on state corruption","Inclination to vote or protest","Invalid vote","Trust in judiciary","Trust in parties","Trust in congress")

colnames(table_appendix) <- c("Variable","Effect","SE","p Value", "N")

#Table A9:
stargazer(table_appendix, out="TableA9.tex")


##treatment outcomes (without covariates):
##Table A10:

outcomes_without_controls <- rbind(
  c("progcorrupt",
    summary(lm(progcorrupt ~ treat14 + factor(ciudad),data=COR))$coefficients[1,1],
    summary(lm(progcorrupt ~ treat14 + factor(ciudad),data=COR))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR,COR$treat14==0|COR$treat14==1)$progcorrupt),1,0))),
  c("participation_willingness",
    summary(lm(participation_willingness ~ treat14 + factor(ciudad),data=COR))$coefficients[1,1],
    summary(lm(participation_willingness ~ treat14 + factor(ciudad),data=COR))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR,COR$treat14==0|COR$treat14==1)$participation_willingness),1,0))),
  c("vote_invalid",
    summary(lm(vote_invalid ~ treat14 + factor(ciudad),data=COR))$coefficients[1,1],
    summary(lm(vote_invalid ~ treat14 + factor(ciudad),data=COR))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR,COR$treat14==0|COR$treat14==1)$vote_invalid),1,0))),
  c("conf_judge",
    summary(lm(conf_judge ~ treat14 + factor(ciudad),data=COR))$coefficients[1,1],
    summary(lm(conf_judge ~ treat14 + factor(ciudad),data=COR))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR,COR$treat14==0|COR$treat14==1)$conf_judge),1,0))),
  c("conf_parties",
    summary(lm(conf_parties ~ treat14 + factor(ciudad),data=COR))$coefficients[1,1],
    summary(lm(conf_parties ~ treat14 + factor(ciudad),data=COR))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR,COR$treat14==0|COR$treat14==1)$conf_parties),1,0))),
  c("conf_parl",
    summary(lm(conf_parl ~ treat14 + factor(ciudad),data=COR))$coefficients[1,1],
    summary(lm(conf_parl ~ treat14 + factor(ciudad),data=COR))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR,COR$treat14==0|COR$treat14==1)$conf_parl),1,0)))
)

colnames(outcomes_without_controls) <- c("Variable",
                                         "Intercept14","Estimate14","SE14","pvalue14","ValidN14")

outcomes_without_controls

table_without_controls <- outcomes_without_controls[,c(1,3:6)]
table_without_controls[,2] <- round(as.numeric(table_without_controls[,2]),3)
table_without_controls[,3] <- round(as.numeric(table_without_controls[,3]),3)
table_without_controls[,4] <- round(as.numeric(table_without_controls[,4]),3) 

table_without_controls[,1] <- c("Progress on corruption","Inclination to vote or protest","Invalid vote","Trust in judiciary","Trust in parties","Trust in congress")

colnames(table_without_controls) <- c("Variable","Effect","SE","p Value", "N")

#Table A10:
stargazer(table_without_controls, out="TableA10.tex")


###CREATE FIGURES
##merge dataframes:
outcomes_without_controls <- as.data.frame(outcomes_without_controls)
outcomes_without_controls$Estimate14 <- as.numeric(as.character(outcomes_without_controls$Estimate14))
outcomes_without_controls$SE14 <- as.numeric(as.character(outcomes_without_controls$SE14))
outcomes_without_controls$pvalue14 <- as.numeric(as.character(outcomes_without_controls$pvalue14))
outcomes_without_controls$Variable <- as.character(outcomes_without_controls$Variable)
outcomes_without_controls$controls <- "Without controls"
outcomes_14 <- as.data.frame(outcomes_without_controls)
colnames(outcomes_14)[2:6] <- c("Intercept","Estimate","SE","pvalue","ValidN")

outcomes_controls <- as.data.frame(outcomes_controls)
outcomes_controls$Estimate14 <- as.numeric(as.character(outcomes_controls$Estimate14))
outcomes_controls$SE14 <- as.numeric(as.character(outcomes_controls$SE14))
outcomes_controls$pvalue14 <- as.numeric(as.character(outcomes_controls$pvalue14))
outcomes_controls$Variable <- as.character(outcomes_controls$Variable)
outcomes_controls$controls <- "With controls"
outcomes_controls_14 <- outcomes_controls
colnames(outcomes_controls_14)[2:6] <- c("Intercept","Estimate","SE","pvalue","ValidN")

outcomes_combined <- rbind(outcomes_14,outcomes_controls_14)

outcomes_combined$Variable <- as.character(outcomes_combined$Variable)
outcomes_combined$Variable[outcomes_combined$Variable=="progcorrupt"] <- "Progress on\ncorruption"
outcomes_combined$Variable[outcomes_combined$Variable=="participation_willingness"] <- "Inclination to\nvote or protest"
outcomes_combined$Variable[outcomes_combined$Variable=="vote_invalid"] <- "Invalid vote"
outcomes_combined$Variable[outcomes_combined$Variable=="conf_judge"] <- "Trust in judiciary"
outcomes_combined$Variable[outcomes_combined$Variable=="conf_parties"] <- "Trust in parties"
outcomes_combined$Variable[outcomes_combined$Variable=="conf_parl"] <- "Trust in congress"
outcomes_combined$Variable <- as.factor(outcomes_combined$Variable)
outcomes_combined$Variable <- factor(outcomes_combined$Variable,levels = c("Progress on\ncorruption","Inclination to\nvote or protest","Invalid vote","Trust in judiciary","Trust in parties","Trust in congress"))


##Figure 6: Trust in Courts, Parl, & Parties:
ggplot(outcomes_combined[c(10:12,4:6),], aes(fill=controls,y=Estimate,x=Variable))+
  ggtitle("")+
  theme(plot.title=element_text(size=16, face="bold", hjust=0.5))+
  theme(plot.margin=unit(c(0.5,0.5,0.5,0.5),"cm"))+
  geom_hline(yintercept=0, linetype="dotted",size=0.9)+
  ylab("Change in\nattitudes/beliefs")+
  xlab("")+
  scale_color_manual(values=c("darkblue", "red"))+
  theme(text=element_text(size=16))+
  theme(axis.text.y=element_text(size=16, color="black"))+
  theme(axis.text.x=element_text(size=16, color="black"))+
  theme(legend.position = "none",legend.title = element_blank())+
  scale_y_continuous(breaks=c(-1,-0.9,-0.8,-0.7,-0.6,-0.5,-0.4,-0.3,-0.2,-0.1,0,0.1,0.2,0.3,0.4,0.5), limits=c(-1,0))+
  theme(legend.position = "bottom",legend.title = element_blank())+
  geom_point(aes(colour=controls,shape=controls),size=4,position = position_dodge(.9))+
  geom_errorbar(aes(ymax=Estimate + (SE*-qnorm((1-0.90)/2)), ymin=Estimate - (SE*-qnorm((1-0.90)/2)),colour=controls), width=.15, position=position_dodge(.9),stat="identity",size=0.8)+
  geom_errorbar(aes(ymax=Estimate + (SE*(-qnorm((1-0.95)/2))), ymin=Estimate - (SE*(-qnorm((1-0.95)/2))),colour=controls), width=.0, position=position_dodge(.9),stat="identity",size=0.8)
ggsave("Figure6.pdf", width = 9, height = 5)


##Figure 7: Inclination to vote or protest & Invalid vote
ggplot(outcomes_combined[c(2:3,8:9),], aes(fill=controls,y=Estimate,x=Variable)) +
  ggtitle("")+
  theme(plot.title=element_text(size=16, face="bold", hjust=0.5))+
  theme(plot.margin=unit(c(0.5,0.5,0.5,0.5),"cm"))+
  geom_hline(yintercept=0, linetype="dotted",size=0.9)+
  ylab("Change in \nbehavior/attitude")+
  xlab("")+
  scale_color_manual(values=c("darkblue", "red"))+
  theme(text=element_text(size=16))+
  theme(axis.text.y=element_text(size=16, color="black"))+
  theme(axis.text.x=element_text(size=16, color="black"))+
  theme(legend.position = "none",legend.title = element_blank())+
  scale_y_continuous(breaks=c(-0.2,-0.1,0,0.1,0.2), limits=c(-0.26,0.25))+
  theme(legend.position = "bottom",legend.title = element_blank())+
  geom_point(aes(colour=controls,shape=controls),size=4,position = position_dodge(.9))+
  geom_errorbar(aes(ymax=Estimate + (SE*-qnorm((1-0.90)/2)), ymin=Estimate - (SE*-qnorm((1-0.90)/2)),colour=controls), width=.15, position=position_dodge(.9),stat="identity",size=0.8)+
  geom_errorbar(aes(ymax=Estimate + (SE*(-qnorm((1-0.95)/2))), ymin=Estimate - (SE*(-qnorm((1-0.95)/2))),colour=controls), width=.0, position=position_dodge(.9),stat="identity",size=0.8)
ggsave("Figure7.pdf", width = 9, height = 4)


##Placebo tests: effect on trust in other institutions:
##Table A22:
other_institutions <- rbind(
  c("conf_church",
    summary(lm(conf_church ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[1,1],
    summary(lm(conf_church ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR,COR$treat14==0|COR$treat14==1)$conf_church),1,0))),
  c("conf_local_gov",
    summary(lm(conf_local_gov ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[1,1],
    summary(lm(conf_local_gov ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR,COR$treat14==0|COR$treat14==1)$conf_local_gov),1,0)))
)

colnames(other_institutions) <- c("Variable",
                                  "Intercept14","Estimate14","SE14","pvalue14","ValidN14")

other_institutions

other_institutions_appendix <- other_institutions[,c(1,3:6)]
other_institutions_appendix[,2] <- round(as.numeric(other_institutions_appendix[,2]),3)
other_institutions_appendix[,3] <- round(as.numeric(other_institutions_appendix[,3]),3)
other_institutions_appendix[,4] <- round(as.numeric(other_institutions_appendix[,4]),3) 

other_institutions_appendix[,1] <- c("Trust in church","Trust in local government")

colnames(other_institutions_appendix) <- c("Variable","Effect","SE","p Value", "N")

#Table A22:
stargazer(other_institutions_appendix, out="TableA22.tex")


##Effects for main outcomes without PUSC supporters:
##Table A12:

COR_withoutPUSC <- subset(COR,p35st!="CR: Partido Unidad Social Cristiana (PUSC)")

outcomes_withoutPUSC <- rbind(
  c("progcorrupt",
    summary(lm(progcorrupt ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR_withoutPUSC))$coefficients[1,1],
    summary(lm(progcorrupt ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR_withoutPUSC))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR_withoutPUSC,COR_withoutPUSC$treat14==0|COR_withoutPUSC$treat14==1)$progcorrupt),1,0))),
  c("participation_willingness",
    summary(lm(participation_willingness ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR_withoutPUSC))$coefficients[1,1],
    summary(lm(participation_willingness ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR_withoutPUSC))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR_withoutPUSC,COR_withoutPUSC$treat14==0|COR_withoutPUSC$treat14==1)$participation_willingness),1,0))),
  c("vote_invalid",
    summary(lm(vote_invalid ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR_withoutPUSC))$coefficients[1,1],
    summary(lm(vote_invalid ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR_withoutPUSC))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR_withoutPUSC,COR_withoutPUSC$treat14==0|COR_withoutPUSC$treat14==1)$vote_invalid),1,0))),
  c("conf_judge",
    summary(lm(conf_judge ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR_withoutPUSC))$coefficients[1,1],
    summary(lm(conf_judge ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR_withoutPUSC))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR_withoutPUSC,COR_withoutPUSC$treat14==0|COR_withoutPUSC$treat14==1)$conf_judge),1,0))),
  c("conf_parties",
    summary(lm(conf_parties ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR_withoutPUSC))$coefficients[1,1],
    summary(lm(conf_parties ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR_withoutPUSC))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR_withoutPUSC,COR_withoutPUSC$treat14==0|COR_withoutPUSC$treat14==1)$conf_parties),1,0))),
  c("conf_parl",
    summary(lm(conf_parl ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR_withoutPUSC))$coefficients[1,1],
    summary(lm(conf_parl ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR_withoutPUSC))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR_withoutPUSC,COR_withoutPUSC$treat14==0|COR_withoutPUSC$treat14==1)$conf_parl),1,0)))
)

colnames(outcomes_withoutPUSC) <- c("Variable",
"Intercept14","Estimate14","SE14","pvalue14","ValidN14")

outcomes_withoutPUSC

table_withoutPUSC <- outcomes_withoutPUSC[,c(1,3:6)]
table_withoutPUSC[,2] <- round(as.numeric(table_withoutPUSC[,2]),3)
table_withoutPUSC[,3] <- round(as.numeric(table_withoutPUSC[,3]),3)
table_withoutPUSC[,4] <- round(as.numeric(table_withoutPUSC[,4]),3) 

table_withoutPUSC[,1] <- c("Progress on corruption","Inclination to vote or protest","Invalid vote","Trust in judiciary","Trust in parties","Trust in congress")

colnames(table_withoutPUSC) <- c("Variable","Effect","SE","p Value", "N")

#Table A12:
stargazer(table_withoutPUSC, out="TableA12.tex")


####Placebo tests
###with alternative cutoff dates:

#the first day of data collection was on Sept. 30, this date is recoded as diareal 0 to ensure that diareal is strictly increasing with data collection
COR$diareal <- as.numeric(as.character(COR$diareal))
COR$diareal[COR$diareal==30] <- 0

set.seed(12022020) #the date on which the randomization occured: 12-02-2020
sample(c(0:19,22),3,replace=F) #yielded 19, 17, 3 as randomly selected cutoff dates within the range of dates during which data collection occured

##for the first cutoff date (October 19, 2009):
d=19
##treatment (before vs. after randomly selected cutoff dates)
COR$placebo14 <- ifelse(COR$diareal<d & COR$diareal>(d-14),0,NA)
COR$placebo14 <- ifelse(COR$diareal>d & COR$diareal<(d+14),1,COR$placebo14)

##treatment outcomes (with covariates):
placebo_outcomes <- rbind(
  c("progcorrupt",
    summary(lm(progcorrupt ~ placebo14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[1,1],
    summary(lm(progcorrupt ~ placebo14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR,COR$placebo14==0|COR$placebo14==1)$progcorrupt),1,0))),
  c("participation_willingness",
    summary(lm(participation_willingness ~ placebo14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[1,1],
    summary(lm(participation_willingness ~ placebo14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR,COR$placebo14==0|COR$placebo14==1)$participation_willingness),1,0))),
  c("vote_invalid",
    summary(lm(vote_invalid ~ placebo14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[1,1],
    summary(lm(vote_invalid ~ placebo14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR,COR$placebo14==0|COR$placebo14==1)$vote_invalid),1,0))),
  c("conf_judge",
    summary(lm(conf_judge ~ placebo14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[1,1],
    summary(lm(conf_judge ~ placebo14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR,COR$placebo14==0|COR$placebo14==1)$conf_judge),1,0))),
  c("conf_parties",
    summary(lm(conf_parties ~ placebo14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[1,1],
    summary(lm(conf_parties ~ placebo14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR,COR$placebo14==0|COR$placebo14==1)$conf_parties),1,0))),
  c("conf_parl",
    summary(lm(conf_parl ~ placebo14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[1,1],
    summary(lm(conf_parl ~ placebo14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR,COR$placebo14==0|COR$placebo14==1)$conf_parl),1,0)))
)

colnames(placebo_outcomes) <- c("Variable",
                                "Intercept14","Estimate14","SE14","pvalue14","ValidN14")

placebo_outcomes
placebo_outcomes <- as.data.frame(placebo_outcomes)

placebo <- placebo_outcomes[,c(1,3:6)]

colnames(placebo) <- c("Variable","Estimate","SE","pvalue", "ValidN")
placebo$Estimate <- as.numeric(as.character(placebo$Estimate))
placebo$SE <- as.numeric(as.character(placebo$SE))

placebo$Variable <- as.character(placebo$Variable)
placebo$Variable[placebo$Variable=="progcorrupt"] <- "Progress on\ncorruption"
placebo$Variable[placebo$Variable=="participation_willingness"] <- "Inclination to\nvote or protest"
placebo$Variable[placebo$Variable=="vote_invalid"] <- "Invalid vote"
placebo$Variable[placebo$Variable=="conf_judge"] <- "Trust in judiciary"
placebo$Variable[placebo$Variable=="conf_parties"] <- "Trust in parties"
placebo$Variable[placebo$Variable=="conf_parl"] <- "Trust in congress"
placebo$Variable <- as.factor(placebo$Variable)
placebo$Variable <- factor(placebo$Variable,levels = c("Progress on\ncorruption","Inclination to\nvote or protest", "Invalid vote", "Trust in judiciary", "Trust in parties", "Trust in congress"))

placebo_19 <- placebo


###for the second cutoff date (October 17, 2009):
d=17
##treatment (before vs. after randomly selected cutoff dates)
COR$placebo14 <- ifelse(COR$diareal<d & COR$diareal>(d-14),0,NA)
COR$placebo14 <- ifelse(COR$diareal>d & COR$diareal<(d+14),1,COR$placebo14)

##treatment outcomes (with covariates):
placebo_outcomes <- rbind(
  c("progcorrupt",
    summary(lm(progcorrupt ~ placebo14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[1,1],
    summary(lm(progcorrupt ~ placebo14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR,COR$placebo14==0|COR$placebo14==1)$progcorrupt),1,0))),
  c("participation_willingness",
    summary(lm(participation_willingness ~ placebo14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[1,1],
    summary(lm(participation_willingness ~ placebo14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR,COR$placebo14==0|COR$placebo14==1)$participation_willingness),1,0))),
  c("vote_invalid",
    summary(lm(vote_invalid ~ placebo14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[1,1],
    summary(lm(vote_invalid ~ placebo14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR,COR$placebo14==0|COR$placebo14==1)$vote_invalid),1,0))),
  c("conf_judge",
    summary(lm(conf_judge ~ placebo14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[1,1],
    summary(lm(conf_judge ~ placebo14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR,COR$placebo14==0|COR$placebo14==1)$conf_judge),1,0))),
  c("conf_parties",
    summary(lm(conf_parties ~ placebo14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[1,1],
    summary(lm(conf_parties ~ placebo14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR,COR$placebo14==0|COR$placebo14==1)$conf_parties),1,0))),
  c("conf_parl",
    summary(lm(conf_parl ~ placebo14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[1,1],
    summary(lm(conf_parl ~ placebo14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR,COR$placebo14==0|COR$placebo14==1)$conf_parl),1,0)))
)

colnames(placebo_outcomes) <- c("Variable",
                                "Intercept14","Estimate14","SE14","pvalue14","ValidN14")

placebo_outcomes
placebo_outcomes <- as.data.frame(placebo_outcomes)

placebo <- placebo_outcomes[,c(1,3:6)]

colnames(placebo) <- c("Variable","Estimate","SE","pvalue", "ValidN")
placebo$Estimate <- as.numeric(as.character(placebo$Estimate))
placebo$SE <- as.numeric(as.character(placebo$SE))

placebo$Variable <- as.character(placebo$Variable)
placebo$Variable[placebo$Variable=="progcorrupt"] <- "Progress on\ncorruption"
placebo$Variable[placebo$Variable=="participation_willingness"] <- "Inclination to\nvote or protest"
placebo$Variable[placebo$Variable=="vote_invalid"] <- "Invalid vote"
placebo$Variable[placebo$Variable=="conf_judge"] <- "Trust in judiciary"
placebo$Variable[placebo$Variable=="conf_parties"] <- "Trust in parties"
placebo$Variable[placebo$Variable=="conf_parl"] <- "Trust in congress"
placebo$Variable <- as.factor(placebo$Variable)
placebo$Variable <- factor(placebo$Variable,levels = c("Progress on\ncorruption","Inclination to\nvote or protest", "Invalid vote", "Trust in judiciary", "Trust in parties", "Trust in congress"))

placebo_17 <- placebo


##for the third cutoff date (October 3, 2009):
d=3
##treatment (before vs. after randomly selected cutoff dates)
COR$placebo14 <- ifelse(COR$diareal<d & COR$diareal>(d-14),0,NA)
COR$placebo14 <- ifelse(COR$diareal>d & COR$diareal<(d+14),1,COR$placebo14)

##treatment outcomes (with covariates):
placebo_outcomes <- rbind(
  c("progcorrupt",
    summary(lm(progcorrupt ~ placebo14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[1,1],
    summary(lm(progcorrupt ~ placebo14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR,COR$placebo14==0|COR$placebo14==1)$progcorrupt),1,0))),
  c("participation_willingness",
    summary(lm(participation_willingness ~ placebo14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[1,1],
    summary(lm(participation_willingness ~ placebo14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR,COR$placebo14==0|COR$placebo14==1)$participation_willingness),1,0))),
  c("vote_invalid",
    summary(lm(vote_invalid ~ placebo14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[1,1],
    summary(lm(vote_invalid ~ placebo14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR,COR$placebo14==0|COR$placebo14==1)$vote_invalid),1,0))),
  c("conf_judge",
    summary(lm(conf_judge ~ placebo14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[1,1],
    summary(lm(conf_judge ~ placebo14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR,COR$placebo14==0|COR$placebo14==1)$conf_judge),1,0))),
  c("conf_parties",
    summary(lm(conf_parties ~ placebo14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[1,1],
    summary(lm(conf_parties ~ placebo14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR,COR$placebo14==0|COR$placebo14==1)$conf_parties),1,0))),
  c("conf_parl",
    summary(lm(conf_parl ~ placebo14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[1,1],
    summary(lm(conf_parl ~ placebo14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR,COR$placebo14==0|COR$placebo14==1)$conf_parl),1,0)))
)

colnames(placebo_outcomes) <- c("Variable",
                                "Intercept14","Estimate14","SE14","pvalue14","ValidN14")

placebo_outcomes
placebo_outcomes <- as.data.frame(placebo_outcomes)

placebo <- placebo_outcomes[,c(1,3:6)]

colnames(placebo) <- c("Variable","Estimate","SE","pvalue", "ValidN")
placebo$Estimate <- as.numeric(as.character(placebo$Estimate))
placebo$SE <- as.numeric(as.character(placebo$SE))

placebo$Variable <- as.character(placebo$Variable)
placebo$Variable[placebo$Variable=="progcorrupt"] <- "Progress on\ncorruption"
placebo$Variable[placebo$Variable=="participation_willingness"] <- "Inclination to\nvote or protest"
placebo$Variable[placebo$Variable=="vote_invalid"] <- "Invalid vote"
placebo$Variable[placebo$Variable=="conf_judge"] <- "Trust in judiciary"
placebo$Variable[placebo$Variable=="conf_parties"] <- "Trust in parties"
placebo$Variable[placebo$Variable=="conf_parl"] <- "Trust in congress"
placebo$Variable <- as.factor(placebo$Variable)
placebo$Variable <- factor(placebo$Variable,levels = c("Progress on\ncorruption","Inclination to\nvote or protest", "Invalid vote", "Trust in judiciary", "Trust in parties", "Trust in congress"))

placebo_3 <- placebo

##to merge the results from the three placebo tests:
placebo_19$date <- "October 19"
placebo_17$date <- "October 17"
placebo_3$date <- "October 3"

placebo <- rbind(placebo_19,placebo_17,placebo_3)

#Figure A11:
ggplot(placebo_19, aes(y=Estimate,x=Variable)) +
  ggtitle("")+
  theme(plot.title=element_text(size=16, face="bold", hjust=0.5))+
  theme(plot.margin=unit(c(0.5,0.5,0.5,0.5),"cm"))+
  geom_hline(yintercept=0, linetype="dotted",size=0.9)+
  ylab("\nTreatment effect")+
  xlab("")+
  theme(axis.text.x = element_text(angle = 90,vjust=0.5,hjust=0.9))+
  scale_color_manual(values=c("darkblue", "red"))+
  theme(text=element_text(size=16))+
  theme(axis.text.y=element_text(size=16, color="black"))+
  theme(axis.text.x=element_text(size=16, color="black"))+
  theme(legend.position = "none",legend.title = element_blank())+
  scale_y_continuous(breaks=c(-1.5,-1.25,-1,-0.75,-0.5,-0.25,0,0.25,0.5,0.75,1,1,1.25,1.5), limits=c(-1.8,1.8))+
  geom_point(size=4,position = position_dodge(.9))+
  geom_errorbar(aes(ymax=Estimate + (SE*-qnorm((1-0.95)/2)), ymin=Estimate - (SE*-qnorm((1-0.95)/2))), width=.1, position=position_dodge(.9),stat="identity",size=0.8)
ggsave("FigureA11.pdf", width = 12, height = 7)

#Figure A12:
ggplot(placebo_17, aes(y=Estimate,x=Variable)) +
  ggtitle("")+
  theme(plot.title=element_text(size=16, face="bold", hjust=0.5))+
  theme(plot.margin=unit(c(0.5,0.5,0.5,0.5),"cm"))+
  geom_hline(yintercept=0, linetype="dotted",size=0.9)+
  ylab("\nTreatment effect")+
  xlab("")+
  theme(axis.text.x = element_text(angle = 90,vjust=0.5,hjust=0.9))+
  scale_color_manual(values=c("darkblue", "red"))+
  theme(text=element_text(size=16))+
  theme(axis.text.y=element_text(size=16, color="black"))+
  theme(axis.text.x=element_text(size=16, color="black"))+
  theme(legend.position = "none",legend.title = element_blank())+
  scale_y_continuous(breaks=c(-1.5,-1.25,-1,-0.75,-0.5,-0.25,0,0.25,0.5,0.75,1,1,1.25,1.5), limits=c(-1.8,1.8))+
  geom_point(size=4,position = position_dodge(.9))+
  geom_errorbar(aes(ymax=Estimate + (SE*-qnorm((1-0.95)/2)), ymin=Estimate - (SE*-qnorm((1-0.95)/2))), width=.1, position=position_dodge(.9),stat="identity",size=0.8)
ggsave("FigureA12.pdf", width = 12, height = 7)

#Figure A13:
ggplot(placebo_3, aes(y=Estimate,x=Variable)) +
  ggtitle("")+
  theme(plot.title=element_text(size=16, face="bold", hjust=0.5))+
  theme(plot.margin=unit(c(0.5,0.5,0.5,0.5),"cm"))+
  geom_hline(yintercept=0, linetype="dotted",size=0.9)+
  ylab("\nTreatment effect")+
  xlab("")+
  theme(axis.text.x = element_text(angle = 90,vjust=0.5,hjust=0.9))+
  scale_color_manual(values=c("darkblue", "red"))+
  theme(text=element_text(size=16))+
  theme(axis.text.y=element_text(size=16, color="black"))+
  theme(axis.text.x=element_text(size=16, color="black"))+
  theme(legend.position = "none",legend.title = element_blank())+
  scale_y_continuous(breaks=c(-1.5,-1.25,-1,-0.75,-0.5,-0.25,0,0.25,0.5,0.75,1,1,1.25,1.5), limits=c(-1.8,1.8))+
  geom_point(size=4,position = position_dodge(.9))+
  geom_errorbar(aes(ymax=Estimate + (SE*-qnorm((1-0.95)/2)), ymin=Estimate - (SE*-qnorm((1-0.95)/2))), width=.1, position=position_dodge(.9),stat="identity",size=0.8)
ggsave("FigureA13.pdf", width = 12, height = 7)



## Placebo test for median of control group (Oct 2):
## Figure A14:

d=2
##treatment (before vs. after median of control group)
COR$placebo <- ifelse(COR$diareal<d,0,NA)
COR$placebo <- ifelse(COR$diareal>d & COR$diareal<5,1,COR$placebo)

placebo_outcomes <- rbind(
  c("progcorrupt",
    summary(lm(progcorrupt ~ placebo + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[1,1],
    summary(lm(progcorrupt ~ placebo + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR,COR$placebo==0|COR$placebo==1)$progcorrupt),1,0))),
  c("participation_willingness",
    summary(lm(participation_willingness ~ placebo + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[1,1],
    summary(lm(participation_willingness ~ placebo + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR,COR$placebo==0|COR$placebo==1)$participation_willingness),1,0))),
  c("vote_invalid",
    summary(lm(vote_invalid ~ placebo + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[1,1],
    summary(lm(vote_invalid ~ placebo + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR,COR$placebo==0|COR$placebo==1)$vote_invalid),1,0))),
  c("conf_judge",
    summary(lm(conf_judge ~ placebo + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[1,1],
    summary(lm(conf_judge ~ placebo + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR,COR$placebo==0|COR$placebo==1)$conf_judge),1,0))),
  c("conf_parties",
    summary(lm(conf_parties ~ placebo + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[1,1],
    summary(lm(conf_parties ~ placebo + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR,COR$placebo==0|COR$placebo==1)$conf_parties),1,0))),
  c("conf_parl",
    summary(lm(conf_parl ~ placebo + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[1,1],
    summary(lm(conf_parl ~ placebo + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR,COR$placebo==0|COR$placebo==1)$conf_parl),1,0)))
)

colnames(placebo_outcomes) <- c("Variable","Intercept","Estimate","SE","pvalue","ValidN")

placebo_outcomes <- as.data.frame(placebo_outcomes)
placebo <- placebo_outcomes

placebo$Estimate <- as.numeric(as.character(placebo$Estimate))
placebo$SE <- as.numeric(as.character(placebo$SE))

placebo$Variable <- as.character(placebo$Variable)
placebo$Variable <- as.character(placebo$Variable)
placebo$Variable[placebo$Variable=="progcorrupt"] <- "Progress on\ncorruption"
placebo$Variable[placebo$Variable=="participation_willingness"] <- "Inclination to\nvote or protest"
placebo$Variable[placebo$Variable=="vote_invalid"] <- "Invalid vote"
placebo$Variable[placebo$Variable=="conf_judge"] <- "Trust in judiciary"
placebo$Variable[placebo$Variable=="conf_parties"] <- "Trust in parties"
placebo$Variable[placebo$Variable=="conf_parl"] <- "Trust in congress"
placebo$Variable <- as.factor(placebo$Variable)
placebo$Variable <- factor(placebo$Variable,levels = c("Progress on\ncorruption","Inclination to\nvote or protest","Invalid vote", "Trust in judiciary", "Trust in parties", "Trust in congress"))

placebo_2 <- placebo

placebo_2$date <- "June 2"

#Figure A14:
ggplot(placebo_2, aes(y=Estimate,x=Variable)) +
  ggtitle("")+
  theme(plot.title=element_text(size=16, face="bold", hjust=0.5))+
  theme(plot.margin=unit(c(0.5,0.5,0.5,0.5),"cm"))+
  geom_hline(yintercept=0, linetype="dotted",size=0.9)+
  ylab("\nTreatment effect")+
  xlab("")+
  theme(axis.text.x = element_text(angle = 90,vjust=0.5,hjust=0.9))+
  scale_color_manual(values=c("darkblue", "red"))+
  theme(text=element_text(size=16))+
  theme(axis.text.y=element_text(size=16, color="black"))+
  theme(axis.text.x=element_text(size=16, color="black"))+
  theme(legend.position = "none",legend.title = element_blank())+
  scale_y_continuous(breaks=c(-1,-0.75,-0.5,-0.25,0,0.25,0.5,0.75,1,1,1.25,1.5), limits=c(-1.2,1))+
  geom_point(size=4,position = position_dodge(.9))+
  geom_errorbar(aes(ymax=Estimate + (SE*-qnorm((1-0.95)/2)), ymin=Estimate - (SE*-qnorm((1-0.95)/2))), width=.1, position=position_dodge(.9),stat="identity",size=0.8)
ggsave("FigureA14.pdf", width = 12, height = 7)


##Checking for time trends in control group:

timetrend_control <- rbind(
  c("progcorrupt",
    summary(lm(progcorrupt ~ diareal + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=subset(COR, placebo==0 | placebo==1)))$coefficients[1,1],
    summary(lm(progcorrupt ~ diareal + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=subset(COR, placebo==0 | placebo==1)))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR,COR$placebo==0 | placebo==1)$progcorrupt),1,0))),
  c("participation_willingness",
    summary(lm(participation_willingness ~ diareal + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=subset(COR, placebo==0 | placebo==1)))$coefficients[1,1],
    summary(lm(participation_willingness ~ diareal + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=subset(COR, placebo==0 | placebo==1)))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR,COR$placebo==0 | placebo==1)$participation_willingness),1,0))),
   c("vote_invalid",
    summary(lm(vote_invalid ~ diareal + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=subset(COR, placebo==0 | placebo==1)))$coefficients[1,1],
    summary(lm(vote_invalid ~ diareal + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=subset(COR, placebo==0 | placebo==1)))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR,COR$placebo==0 | placebo==1)$vote_invalid),1,0))),
  c("conf_judge",
    summary(lm(conf_judge ~ diareal + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=subset(COR, placebo==0 | placebo==1)))$coefficients[1,1],
    summary(lm(conf_judge ~ diareal + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=subset(COR, placebo==0 | placebo==1)))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR,COR$placebo==0 | placebo==1)$conf_judge),1,0))),
  c("conf_parties",
    summary(lm(conf_parties ~ diareal + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=subset(COR, placebo==0 | placebo==1)))$coefficients[1,1],
    summary(lm(conf_parties ~ diareal + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=subset(COR, placebo==0 | placebo==1)))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR,COR$placebo==0 | placebo==1)$conf_parties),1,0))),
  c("conf_parl",
    summary(lm(conf_parl ~ diareal + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=subset(COR, placebo==0 | placebo==1)))$coefficients[1,1],
    summary(lm(conf_parl ~ diareal + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=subset(COR, placebo==0 | placebo==1)))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(COR,COR$placebo==0 | placebo==1)$conf_parl),1,0)))
)

colnames(timetrend_control) <- c("Variable","Intercept","Estimate","SE","pvalue","ValidN")

timetrend_control <- as.data.frame(timetrend_control)
timetrend_control



####Het. effects:

###by gender:
###Table A18:

##men:
men <- subset(COR, COR$male==1)

outcomes_men <- rbind(
  c("progcorrupt",
    summary(lm(progcorrupt ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=men))$coefficients[1,1],
    summary(lm(progcorrupt ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=men))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(men,men$treat14==0|men$treat14==1)$progcorrupt),1,0))),
  c("participation_willingness",
    summary(lm(participation_willingness ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=men))$coefficients[1,1],
    summary(lm(participation_willingness ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=men))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(men,men$treat14==0|men$treat14==1)$participation_willingness),1,0))),
  c("vote_invalid",
    summary(lm(vote_invalid ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=men))$coefficients[1,1],
    summary(lm(vote_invalid ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=men))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(men,men$treat14==0|men$treat14==1)$vote_invalid),1,0))),
  c("conf_judge",
    summary(lm(conf_judge ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=men))$coefficients[1,1],
    summary(lm(conf_judge ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=men))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(men,men$treat14==0|men$treat14==1)$conf_judge),1,0))),
  c("conf_parties",
    summary(lm(conf_parties ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=men))$coefficients[1,1],
    summary(lm(conf_parties ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=men))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(men,men$treat14==0|men$treat14==1)$conf_parties),1,0))),
  c("conf_parl",
    summary(lm(conf_parl ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=men))$coefficients[1,1],
    summary(lm(conf_parl ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=men))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(men,men$treat14==0|men$treat14==1)$conf_parl),1,0)))
)

colnames(outcomes_men) <- c("Variable",
                            "Intercept14","Estimate14","SE14","pvalue14","ValidN14")

het_men <- outcomes_men[,c(1,3,4:6)]
het_men[,2] <- round(as.numeric(het_men[,2]),3)
het_men[,3] <- round(as.numeric(het_men[,3]),3)
het_men[,4] <- round(as.numeric(het_men[,4]),3) 

het_men <- as.data.frame(het_men)

het_men[,1] <- c("Progress on corruption","Inclination to vote or protest","Invalid vote","Trust in judiciary","Trust in parties","Trust in congress")

colnames(het_men) <- c("Variable","Effect","SE","p Value", "N")

het_men


##female:
female <- subset(COR, COR$male==0)

outcomes_female <- rbind(
  c("progcorrupt",
    summary(lm(progcorrupt ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=female))$coefficients[1,1],
    summary(lm(progcorrupt ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=female))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(female,female$treat14==0|female$treat14==1)$progcorrupt),1,0))),
  c("participation_willingness",
    summary(lm(participation_willingness ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=female))$coefficients[1,1],
    summary(lm(participation_willingness ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=female))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(female,female$treat14==0|female$treat14==1)$participation_willingness),1,0))),
  c("vote_invalid",
    summary(lm(vote_invalid ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=female))$coefficients[1,1],
    summary(lm(vote_invalid ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=female))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(female,female$treat14==0|female$treat14==1)$vote_invalid),1,0))),
  c("conf_judge",
    summary(lm(conf_judge ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=female))$coefficients[1,1],
    summary(lm(conf_judge ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=female))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(female,female$treat14==0|female$treat14==1)$conf_judge),1,0))),
  c("conf_parties",
    summary(lm(conf_parties ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=female))$coefficients[1,1],
    summary(lm(conf_parties ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=female))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(female,female$treat14==0|female$treat14==1)$conf_parties),1,0))),
  c("conf_parl",
    summary(lm(conf_parl ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=female))$coefficients[1,1],
    summary(lm(conf_parl ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=female))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(female,female$treat14==0|female$treat14==1)$conf_parl),1,0)))
)

colnames(outcomes_female) <- c("Variable",
                               "Intercept14","Estimate14","SE14","pvalue14","ValidN14")

het_women <- outcomes_female[,c(1,3,4:6)]
het_women[,2] <- round(as.numeric(het_women[,2]),3)
het_women[,3] <- round(as.numeric(het_women[,3]),3)
het_women[,4] <- round(as.numeric(het_women[,4]),3) 

het_women <- as.data.frame(het_women)

het_women[,1] <- c("Progress on corruption","Inclination to vote or protest","Invalid vote","Trust in judiciary","Trust in parties","Trust in congress")

colnames(het_women) <- c("Variable","Effect","SE","p Value", "N")

het_women


##interact_gender:
valid_gender <- subset(COR, !is.na(male))

outcomes_interact_gender <- rbind(
  c("progcorrupt",
    summary(lm(progcorrupt ~ treat14 * male + treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=valid_gender))$coefficients[1,1],
    summary(lm(progcorrupt ~ treat14 * male + treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=valid_gender))$coefficients[32,][c(1,2,4)],
    sum(ifelse(!is.na(subset(valid_gender,valid_gender$treat14==0|valid_gender$treat14==1)$progcorrupt),1,0))),
  c("participation_willingness",
    summary(lm(participation_willingness ~ treat14 * male + treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=valid_gender))$coefficients[1,1],
    summary(lm(participation_willingness ~ treat14 * male + treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=valid_gender))$coefficients[32,][c(1,2,4)],
    sum(ifelse(!is.na(subset(valid_gender,valid_gender$treat14==0|valid_gender$treat14==1)$participation_willingness),1,0))),
  c("vote_invalid",
    summary(lm(vote_invalid ~ treat14 * male + treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=valid_gender))$coefficients[1,1],
    summary(lm(vote_invalid ~ treat14 * male + treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=valid_gender))$coefficients[32,][c(1,2,4)],
    sum(ifelse(!is.na(subset(valid_gender,valid_gender$treat14==0|valid_gender$treat14==1)$vote_invalid),1,0))),
  c("conf_judge",
    summary(lm(conf_judge ~ treat14 * male + treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=valid_gender))$coefficients[1,1],
    summary(lm(conf_judge ~ treat14 * male + treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=valid_gender))$coefficients[32,][c(1,2,4)],
    sum(ifelse(!is.na(subset(valid_gender,valid_gender$treat14==0|valid_gender$treat14==1)$conf_judge),1,0))),
  c("conf_parties",
    summary(lm(conf_parties ~ treat14 * male + treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=valid_gender))$coefficients[1,1],
    summary(lm(conf_parties ~ treat14 * male + treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=valid_gender))$coefficients[32,][c(1,2,4)],
    sum(ifelse(!is.na(subset(valid_gender,valid_gender$treat14==0|valid_gender$treat14==1)$conf_parties),1,0))),
  c("conf_parl",
    summary(lm(conf_parl ~ treat14 * male + treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=valid_gender))$coefficients[1,1],
    summary(lm(conf_parl ~ treat14 * male + treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=valid_gender))$coefficients[32,][c(1,2,4)],
    sum(ifelse(!is.na(subset(valid_gender,valid_gender$treat14==0|valid_gender$treat14==1)$conf_parl),1,0)))
)

colnames(outcomes_interact_gender) <- c("Variable",
                                        "Intercept14","Estimate14","SE14","pvalue14","ValidN14")

het_interact_gender <- outcomes_interact_gender[,c(1,3,4:6)]
het_interact_gender[,2] <- round(as.numeric(het_interact_gender[,2]),3)
het_interact_gender[,3] <- round(as.numeric(het_interact_gender[,3]),3)
het_interact_gender[,4] <- round(as.numeric(het_interact_gender[,4]),3) 

het_interact_gender <- as.data.frame(het_interact_gender)

het_interact_gender[,1] <-  c("Progress on corruption","Inclination to vote or protest","Invalid vote","Trust in judiciary","Trust in parties","Trust in congress")

colnames(het_interact_gender) <- c("Variable","Effect","SE","p Value", "N")

het_interact_gender

##combine the output for the different subgroup analyses & interaction term:
COR_het_men <- as.data.frame(cbind(rep(NA,2*nrow(het_women)),rep(NA,2*nrow(het_women)),rep(NA,2*nrow(het_women)),rep(NA,2*nrow(het_women))))

COR_het_men[1,1] <- het_women[1,1]
COR_het_men[2,1] <- ""
COR_het_men[3,1] <- het_women[2,1]
COR_het_men[4,1] <- ""
COR_het_men[5,1] <- het_women[3,1]
COR_het_men[6,1] <- ""
COR_het_men[7,1] <- het_women[4,1]
COR_het_men[8,1] <- ""
COR_het_men[9,1] <- het_women[5,1]
COR_het_men[10,1] <- ""
COR_het_men[11,1] <- het_women[6,1]
COR_het_men[12,1] <- ""
COR_het_men[13,1] <- "Sample"

het_women[,2] <- as.numeric(as.character(het_women[,2]))
het_women[,3] <- as.numeric(as.character(het_women[,3]))
het_women[,4] <- as.numeric(as.character(het_women[,4]))

COR_het_men[13,2] <- "Women"
COR_het_men[1,2] <- ifelse(het_women[1,4]<0.1,paste0(het_women[1,2],"*"),het_women[1,2])
COR_het_men[1,2] <- ifelse(het_women[1,4]<0.05,paste0(het_women[1,2],"**"),COR_het_men[1,2])
COR_het_men[1,2] <- ifelse(het_women[1,4]<0.01,paste0(het_women[1,2],"***"),COR_het_men[1,2])

COR_het_men[3,2] <- ifelse(het_women[2,4]<0.1,paste0(het_women[2,2],"*"),het_women[2,2])
COR_het_men[3,2] <- ifelse(het_women[2,4]<0.05,paste0(het_women[2,2],"**"),COR_het_men[3,2])
COR_het_men[3,2] <- ifelse(het_women[2,4]<0.01,paste0(het_women[2,2],"***"),COR_het_men[3,2])

COR_het_men[5,2] <- ifelse(het_women[3,4]<0.1,paste0(het_women[3,2],"*"),het_women[3,2])
COR_het_men[5,2] <- ifelse(het_women[3,4]<0.05,paste0(het_women[3,2],"**"),COR_het_men[5,2])
COR_het_men[5,2] <- ifelse(het_women[3,4]<0.01,paste0(het_women[3,2],"***"),COR_het_men[5,2])

COR_het_men[7,2] <- ifelse(het_women[4,4]<0.1,paste0(het_women[4,2],"*"),het_women[4,2])
COR_het_men[7,2] <- ifelse(het_women[4,4]<0.05,paste0(het_women[4,2],"**"),COR_het_men[7,2])
COR_het_men[7,2] <- ifelse(het_women[4,4]<0.01,paste0(het_women[4,2],"***"),COR_het_men[7,2])

COR_het_men[9,2] <- ifelse(het_women[5,4]<0.1,paste0(het_women[5,2],"*"),het_women[5,2])
COR_het_men[9,2] <- ifelse(het_women[5,4]<0.05,paste0(het_women[5,2],"**"),COR_het_men[9,2])
COR_het_men[9,2] <- ifelse(het_women[5,4]<0.01,paste0(het_women[5,2],"***"),COR_het_men[9,2])

COR_het_men[11,2] <- ifelse(het_women[6,4]<0.1,paste0(het_women[6,2],"*"),het_women[6,2])
COR_het_men[11,2] <- ifelse(het_women[6,4]<0.05,paste0(het_women[6,2],"**"),COR_het_men[11,2])
COR_het_men[11,2] <- ifelse(het_women[6,4]<0.01,paste0(het_women[6,2],"***"),COR_het_men[11,2])

COR_het_men[2,2] <- paste0("(",het_women[1,3],")")
COR_het_men[4,2] <- paste0("(",het_women[2,3],")")
COR_het_men[6,2] <- paste0("(",het_women[3,3],")")
COR_het_men[8,2] <- paste0("(",het_women[4,3],")")
COR_het_men[10,2] <- paste0("(",het_women[5,3],")")
COR_het_men[12,2] <- paste0("(",het_women[6,3],")")

het_men[,2] <- as.numeric(as.character(het_men[,2]))
het_men[,3] <- as.numeric(as.character(het_men[,3]))
het_men[,4] <- as.numeric(as.character(het_men[,4]))

COR_het_men[13,3] <- "Men"
COR_het_men[1,3] <- ifelse(het_men[1,4]<0.1,paste0(het_men[1,2],"*"),het_men[1,2])
COR_het_men[1,3] <- ifelse(het_men[1,4]<0.05,paste0(het_men[1,2],"**"),COR_het_men[1,3])
COR_het_men[1,3] <- ifelse(het_men[1,4]<0.01,paste0(het_men[1,2],"***"),COR_het_men[1,3])

COR_het_men[3,3] <- ifelse(het_men[2,4]<0.1,paste0(het_men[2,2],"*"),het_men[2,2])
COR_het_men[3,3] <- ifelse(het_men[2,4]<0.05,paste0(het_men[2,2],"**"),COR_het_men[3,3])
COR_het_men[3,3] <- ifelse(het_men[2,4]<0.01,paste0(het_men[2,2],"***"),COR_het_men[3,3])

COR_het_men[5,3] <- ifelse(het_men[3,4]<0.1,paste0(het_men[3,2],"*"),het_men[3,2])
COR_het_men[5,3] <- ifelse(het_men[3,4]<0.05,paste0(het_men[3,2],"**"),COR_het_men[5,3])
COR_het_men[5,3] <- ifelse(het_men[3,4]<0.01,paste0(het_men[3,2],"***"),COR_het_men[5,3])

COR_het_men[7,3] <- ifelse(het_men[4,4]<0.1,paste0(het_men[4,2],"*"),het_men[4,2])
COR_het_men[7,3] <- ifelse(het_men[4,4]<0.05,paste0(het_men[4,2],"**"),COR_het_men[7,3])
COR_het_men[7,3] <- ifelse(het_men[4,4]<0.01,paste0(het_men[4,2],"***"),COR_het_men[7,3])

COR_het_men[9,3] <- ifelse(het_men[5,4]<0.1,paste0(het_men[5,2],"*"),het_men[5,2])
COR_het_men[9,3] <- ifelse(het_men[5,4]<0.05,paste0(het_men[5,2],"**"),COR_het_men[9,3])
COR_het_men[9,3] <- ifelse(het_men[5,4]<0.01,paste0(het_men[5,2],"***"),COR_het_men[9,3])

COR_het_men[11,3] <- ifelse(het_men[6,4]<0.1,paste0(het_men[6,2],"*"),het_men[6,2])
COR_het_men[11,3] <- ifelse(het_men[6,4]<0.05,paste0(het_men[6,2],"**"),COR_het_men[11,3])
COR_het_men[11,3] <- ifelse(het_men[6,4]<0.01,paste0(het_men[6,2],"***"),COR_het_men[11,3])

COR_het_men[2,3] <- paste0("(",het_men[1,3],")")
COR_het_men[4,3] <- paste0("(",het_men[2,3],")")
COR_het_men[6,3] <- paste0("(",het_men[3,3],")")
COR_het_men[8,3] <- paste0("(",het_men[4,3],")")
COR_het_men[10,3] <- paste0("(",het_men[5,3],")")
COR_het_men[12,3] <- paste0("(",het_men[6,3],")")

het_interact_gender[,2] <- as.numeric(as.character(het_interact_gender[,2]))
het_interact_gender[,3] <- as.numeric(as.character(het_interact_gender[,3]))
het_interact_gender[,4] <- as.numeric(as.character(het_interact_gender[,4]))

COR_het_men[13,4] <- "Interaction Term"
COR_het_men[1,4] <- ifelse(het_interact_gender[1,4]<0.1,paste0(het_interact_gender[1,2],"*"),het_interact_gender[1,2])
COR_het_men[1,4] <- ifelse(het_interact_gender[1,4]<0.05,paste0(het_interact_gender[1,2],"**"),COR_het_men[1,4])
COR_het_men[1,4] <- ifelse(het_interact_gender[1,4]<0.01,paste0(het_interact_gender[1,2],"***"),COR_het_men[1,4])

COR_het_men[3,4] <- ifelse(het_interact_gender[2,4]<0.1,paste0(het_interact_gender[2,2],"*"),het_interact_gender[2,2])
COR_het_men[3,4] <- ifelse(het_interact_gender[2,4]<0.05,paste0(het_interact_gender[2,2],"**"),COR_het_men[3,4])
COR_het_men[3,4] <- ifelse(het_interact_gender[2,4]<0.01,paste0(het_interact_gender[2,2],"***"),COR_het_men[3,4])

COR_het_men[5,4] <- ifelse(het_interact_gender[3,4]<0.1,paste0(het_interact_gender[3,2],"*"),het_interact_gender[3,2])
COR_het_men[5,4] <- ifelse(het_interact_gender[3,4]<0.05,paste0(het_interact_gender[3,2],"**"),COR_het_men[5,4])
COR_het_men[5,4] <- ifelse(het_interact_gender[3,4]<0.01,paste0(het_interact_gender[3,2],"***"),COR_het_men[5,4])

COR_het_men[7,4] <- ifelse(het_interact_gender[4,4]<0.1,paste0(het_interact_gender[4,2],"*"),het_interact_gender[4,2])
COR_het_men[7,4] <- ifelse(het_interact_gender[4,4]<0.05,paste0(het_interact_gender[4,2],"**"),COR_het_men[7,4])
COR_het_men[7,4] <- ifelse(het_interact_gender[4,4]<0.01,paste0(het_interact_gender[4,2],"***"),COR_het_men[7,4])

COR_het_men[9,4] <- ifelse(het_interact_gender[5,4]<0.1,paste0(het_interact_gender[5,2],"*"),het_interact_gender[5,2])
COR_het_men[9,4] <- ifelse(het_interact_gender[5,4]<0.05,paste0(het_interact_gender[5,2],"**"),COR_het_men[9,4])
COR_het_men[9,4] <- ifelse(het_interact_gender[5,4]<0.01,paste0(het_interact_gender[5,2],"***"),COR_het_men[9,4])

COR_het_men[11,4] <- ifelse(het_interact_gender[6,4]<0.1,paste0(het_interact_gender[6,2],"*"),het_interact_gender[6,2])
COR_het_men[11,4] <- ifelse(het_interact_gender[6,4]<0.05,paste0(het_interact_gender[6,2],"**"),COR_het_men[11,4])
COR_het_men[11,4] <- ifelse(het_interact_gender[6,4]<0.01,paste0(het_interact_gender[6,2],"***"),COR_het_men[11,4])

COR_het_men[2,4] <- paste0("(",het_interact_gender[1,3],")")
COR_het_men[4,4] <- paste0("(",het_interact_gender[2,3],")")
COR_het_men[6,4] <- paste0("(",het_interact_gender[3,3],")")
COR_het_men[8,4] <- paste0("(",het_interact_gender[4,3],")")
COR_het_men[10,4] <- paste0("(",het_interact_gender[5,3],")")
COR_het_men[12,4] <- paste0("(",het_interact_gender[6,3],")")

COR_het_men

#Table A18:
stargazer(as.matrix(COR_het_men), rownames=F, out="TableA18.tex")


###by region:
##Table A17:

##conurbano:
conurbano <- subset(COR, COR$reg=="CR: San José" | COR$reg=="CR: Alajuela" | COR$reg=="CR: Heredia")
COR$conurbano <- ifelse(COR$reg=="CR: San José" | COR$reg=="CR: Alajuela" | COR$reg=="CR: Heredia", 1, 0)

outcomes_conurbano <- rbind(
  c("progcorrupt",
    summary(lm(progcorrupt ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=conurbano))$coefficients[1,1],
    summary(lm(progcorrupt ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=conurbano))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(conurbano,treat14==0|treat14==1)$progcorrupt),1,0))),
  c("participation_willingness",
    summary(lm(participation_willingness ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=conurbano))$coefficients[1,1],
    summary(lm(participation_willingness ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=conurbano))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(conurbano,treat14==0|treat14==1)$participation_willingness),1,0))),
  c("vote_invalid",
    summary(lm(vote_invalid ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=conurbano))$coefficients[1,1],
    summary(lm(vote_invalid ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=conurbano))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(conurbano,treat14==0|treat14==1)$vote_invalid),1,0))),
  c("conf_judge",
    summary(lm(conf_judge ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=conurbano))$coefficients[1,1],
    summary(lm(conf_judge ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=conurbano))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(conurbano,treat14==0|treat14==1)$conf_judge),1,0))),
  c("conf_parties",
    summary(lm(conf_parties ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=conurbano))$coefficients[1,1],
    summary(lm(conf_parties ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=conurbano))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(conurbano,treat14==0|treat14==1)$conf_parties),1,0))),
  c("conf_parl",
    summary(lm(conf_parl ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=conurbano))$coefficients[1,1],
    summary(lm(conf_parl ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=conurbano))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(conurbano,treat14==0|treat14==1)$conf_parl),1,0)))
)

colnames(outcomes_conurbano) <- c("Variable",
                                  "Intercept14","Estimate14","SE14","pvalue14","ValidN14")

het_conurbano <- outcomes_conurbano[,c(1,3,4:6)]
het_conurbano[,2] <- round(as.numeric(het_conurbano[,2]),3)
het_conurbano[,3] <- round(as.numeric(het_conurbano[,3]),3)
het_conurbano[,4] <- round(as.numeric(het_conurbano[,4]),3) 

het_conurbano <- as.data.frame(het_conurbano)

het_conurbano[,1] <- c("Progress on corruption","Inclination to vote or protest","Invalid vote","Trust in judiciary","Trust in parties","Trust in congress")

colnames(het_conurbano) <- c("Variable","Effect","SE","p Value", "N")

het_conurbano


##other_provinces:
other_provinces <- subset(COR, COR$reg!="CR: San José" & COR$reg!="CR: Alajuela" & COR$reg!="CR: Heredia")

outcomes_other_provinces <- rbind(
  c("progcorrupt",
    summary(lm(progcorrupt ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=other_provinces))$coefficients[1,1],
    summary(lm(progcorrupt ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=other_provinces))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(other_provinces,other_provinces$treat14==0|other_provinces$treat14==1)$progcorrupt),1,0))),
  c("participation_willingness",
    summary(lm(participation_willingness ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=other_provinces))$coefficients[1,1],
    summary(lm(participation_willingness ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=other_provinces))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(other_provinces,other_provinces$treat14==0|other_provinces$treat14==1)$participation_willingness),1,0))),
  c("vote_invalid",
    summary(lm(vote_invalid ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=other_provinces))$coefficients[1,1],
    summary(lm(vote_invalid ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=other_provinces))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(other_provinces,other_provinces$treat14==0|other_provinces$treat14==1)$vote_invalid),1,0))),
  c("conf_judge",
    summary(lm(conf_judge ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=other_provinces))$coefficients[1,1],
    summary(lm(conf_judge ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=other_provinces))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(other_provinces,other_provinces$treat14==0|other_provinces$treat14==1)$conf_judge),1,0))),
  c("conf_parties",
    summary(lm(conf_parties ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=other_provinces))$coefficients[1,1],
    summary(lm(conf_parties ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=other_provinces))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(other_provinces,other_provinces$treat14==0|other_provinces$treat14==1)$conf_parties),1,0))),
  c("conf_parl",
    summary(lm(conf_parl ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=other_provinces))$coefficients[1,1],
    summary(lm(conf_parl ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=other_provinces))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(other_provinces,other_provinces$treat14==0|other_provinces$treat14==1)$conf_parl),1,0)))
)

colnames(outcomes_other_provinces) <- c("Variable",
                                        "Intercept14","Estimate14","SE14","pvalue14","ValidN14")

het_other_provinces <- outcomes_other_provinces[,c(1,3,4:6)]
het_other_provinces[,2] <- round(as.numeric(het_other_provinces[,2]),3)
het_other_provinces[,3] <- round(as.numeric(het_other_provinces[,3]),3)
het_other_provinces[,4] <- round(as.numeric(het_other_provinces[,4]),3) 

het_other_provinces <- as.data.frame(het_other_provinces)

het_other_provinces[,1] <- c("Progress on corruption","Inclination to vote or protest","Invalid vote","Trust in judiciary","Trust in parties","Trust in congress")

colnames(het_other_provinces) <- c("Variable","Effect","SE","p Value", "N")

het_other_provinces


##interact_region:
valid_region <- subset(COR, !is.na(conurbano))

outcomes_interact_region <- rbind(
  c("progcorrupt",
    summary(lm(progcorrupt ~ treat14 * conurbano + treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=valid_region))$coefficients[1,1],
    summary(lm(progcorrupt ~ treat14 * conurbano + treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=valid_region))$coefficients[32,][c(1,2,4)],
    sum(ifelse(!is.na(subset(valid_region,valid_region$treat14==0|valid_region$treat14==1)$progcorrupt),1,0))),
  c("participation_willingness",
    summary(lm(participation_willingness ~ treat14 * conurbano + treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=valid_region))$coefficients[1,1],
    summary(lm(participation_willingness ~ treat14 * conurbano + treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=valid_region))$coefficients[32,][c(1,2,4)],
    sum(ifelse(!is.na(subset(valid_region,valid_region$treat14==0|valid_region$treat14==1)$participation_willingness),1,0))),
  c("vote_invalid",
    summary(lm(vote_invalid ~ treat14 * conurbano + treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=valid_region))$coefficients[1,1],
    summary(lm(vote_invalid ~ treat14 * conurbano + treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=valid_region))$coefficients[32,][c(1,2,4)],
    sum(ifelse(!is.na(subset(valid_region,valid_region$treat14==0|valid_region$treat14==1)$vote_invalid),1,0))),
  c("conf_judge",
    summary(lm(conf_judge ~ treat14 * conurbano + treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=valid_region))$coefficients[1,1],
    summary(lm(conf_judge ~ treat14 * conurbano + treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=valid_region))$coefficients[32,][c(1,2,4)],
    sum(ifelse(!is.na(subset(valid_region,valid_region$treat14==0|valid_region$treat14==1)$conf_judge),1,0))),
  c("conf_parties",
    summary(lm(conf_parties ~ treat14 * conurbano + treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=valid_region))$coefficients[1,1],
    summary(lm(conf_parties ~ treat14 * conurbano + treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=valid_region))$coefficients[32,][c(1,2,4)],
    sum(ifelse(!is.na(subset(valid_region,valid_region$treat14==0|valid_region$treat14==1)$conf_parties),1,0))),
  c("conf_parl",
    summary(lm(conf_parl ~ treat14 * conurbano + treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=valid_region))$coefficients[1,1],
    summary(lm(conf_parl ~ treat14 * conurbano + treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=valid_region))$coefficients[32,][c(1,2,4)],
    sum(ifelse(!is.na(subset(valid_region,valid_region$treat14==0|valid_region$treat14==1)$conf_parl),1,0)))
)

colnames(outcomes_interact_region) <- c("Variable",
"Intercept14","Estimate14","SE14","pvalue14","ValidN14")

het_interact_region <- outcomes_interact_region[,c(1,3,4:6)]
het_interact_region[,2] <- round(as.numeric(het_interact_region[,2]),3)
het_interact_region[,3] <- round(as.numeric(het_interact_region[,3]),3)
het_interact_region[,4] <- round(as.numeric(het_interact_region[,4]),3) 

het_interact_region <- as.data.frame(het_interact_region)

het_interact_region[,1] <-  c("Progress on corruption","Inclination to vote or protest","Invalid vote","Trust in judiciary","Trust in parties","Trust in congress")

colnames(het_interact_region) <- c("Variable","Effect","SE","p Value", "N")

het_interact_region

##combine the output for the different subgroup analyses & interaction term:
COR_het_region <- as.data.frame(cbind(rep(NA,2*nrow(het_other_provinces)),rep(NA,2*nrow(het_other_provinces)),rep(NA,2*nrow(het_other_provinces)),rep(NA,2*nrow(het_other_provinces))))

COR_het_region[1,1] <- het_other_provinces[1,1]
COR_het_region[2,1] <- ""
COR_het_region[3,1] <- het_other_provinces[2,1]
COR_het_region[4,1] <- ""
COR_het_region[5,1] <- het_other_provinces[3,1]
COR_het_region[6,1] <- ""
COR_het_region[7,1] <- het_other_provinces[4,1]
COR_het_region[8,1] <- ""
COR_het_region[9,1] <- het_other_provinces[5,1]
COR_het_region[10,1] <- ""
COR_het_region[11,1] <- het_other_provinces[6,1]
COR_het_region[12,1] <- ""
COR_het_region[13,1] <- "Sample"

het_other_provinces[,2] <- as.numeric(as.character(het_other_provinces[,2]))
het_other_provinces[,3] <- as.numeric(as.character(het_other_provinces[,3]))
het_other_provinces[,4] <- as.numeric(as.character(het_other_provinces[,4]))

COR_het_region[13,2] <- "Other Provinces"
COR_het_region[1,2] <- ifelse(het_other_provinces[1,4]<0.1,paste0(het_other_provinces[1,2],"*"),het_other_provinces[1,2])
COR_het_region[1,2] <- ifelse(het_other_provinces[1,4]<0.05,paste0(het_other_provinces[1,2],"**"),COR_het_region[1,2])
COR_het_region[1,2] <- ifelse(het_other_provinces[1,4]<0.01,paste0(het_other_provinces[1,2],"***"),COR_het_region[1,2])

COR_het_region[3,2] <- ifelse(het_other_provinces[2,4]<0.1,paste0(het_other_provinces[2,2],"*"),het_other_provinces[2,2])
COR_het_region[3,2] <- ifelse(het_other_provinces[2,4]<0.05,paste0(het_other_provinces[2,2],"**"),COR_het_region[3,2])
COR_het_region[3,2] <- ifelse(het_other_provinces[2,4]<0.01,paste0(het_other_provinces[2,2],"***"),COR_het_region[3,2])

COR_het_region[5,2] <- ifelse(het_other_provinces[3,4]<0.1,paste0(het_other_provinces[3,2],"*"),het_other_provinces[3,2])
COR_het_region[5,2] <- ifelse(het_other_provinces[3,4]<0.05,paste0(het_other_provinces[3,2],"**"),COR_het_region[5,2])
COR_het_region[5,2] <- ifelse(het_other_provinces[3,4]<0.01,paste0(het_other_provinces[3,2],"***"),COR_het_region[5,2])

COR_het_region[7,2] <- ifelse(het_other_provinces[4,4]<0.1,paste0(het_other_provinces[4,2],"*"),het_other_provinces[4,2])
COR_het_region[7,2] <- ifelse(het_other_provinces[4,4]<0.05,paste0(het_other_provinces[4,2],"**"),COR_het_region[7,2])
COR_het_region[7,2] <- ifelse(het_other_provinces[4,4]<0.01,paste0(het_other_provinces[4,2],"***"),COR_het_region[7,2])

COR_het_region[9,2] <- ifelse(het_other_provinces[5,4]<0.1,paste0(het_other_provinces[5,2],"*"),het_other_provinces[5,2])
COR_het_region[9,2] <- ifelse(het_other_provinces[5,4]<0.05,paste0(het_other_provinces[5,2],"**"),COR_het_region[9,2])
COR_het_region[9,2] <- ifelse(het_other_provinces[5,4]<0.01,paste0(het_other_provinces[5,2],"***"),COR_het_region[9,2])

COR_het_region[11,2] <- ifelse(het_other_provinces[6,4]<0.1,paste0(het_other_provinces[6,2],"*"),het_other_provinces[6,2])
COR_het_region[11,2] <- ifelse(het_other_provinces[6,4]<0.05,paste0(het_other_provinces[6,2],"**"),COR_het_region[11,2])
COR_het_region[11,2] <- ifelse(het_other_provinces[6,4]<0.01,paste0(het_other_provinces[6,2],"***"),COR_het_region[11,2])

COR_het_region[2,2] <- paste0("(",het_other_provinces[1,3],")")
COR_het_region[4,2] <- paste0("(",het_other_provinces[2,3],")")
COR_het_region[6,2] <- paste0("(",het_other_provinces[3,3],")")
COR_het_region[8,2] <- paste0("(",het_other_provinces[4,3],")")
COR_het_region[10,2] <- paste0("(",het_other_provinces[5,3],")")
COR_het_region[12,2] <- paste0("(",het_other_provinces[6,3],")")

het_conurbano[,2] <- as.numeric(as.character(het_conurbano[,2]))
het_conurbano[,3] <- as.numeric(as.character(het_conurbano[,3]))
het_conurbano[,4] <- as.numeric(as.character(het_conurbano[,4]))

COR_het_region[13,3] <- "Conurbano"
COR_het_region[1,3] <- ifelse(het_conurbano[1,4]<0.1,paste0(het_conurbano[1,2],"*"),het_conurbano[1,2])
COR_het_region[1,3] <- ifelse(het_conurbano[1,4]<0.05,paste0(het_conurbano[1,2],"**"),COR_het_region[1,3])
COR_het_region[1,3] <- ifelse(het_conurbano[1,4]<0.01,paste0(het_conurbano[1,2],"***"),COR_het_region[1,3])

COR_het_region[3,3] <- ifelse(het_conurbano[2,4]<0.1,paste0(het_conurbano[2,2],"*"),het_conurbano[2,2])
COR_het_region[3,3] <- ifelse(het_conurbano[2,4]<0.05,paste0(het_conurbano[2,2],"**"),COR_het_region[3,3])
COR_het_region[3,3] <- ifelse(het_conurbano[2,4]<0.01,paste0(het_conurbano[2,2],"***"),COR_het_region[3,3])

COR_het_region[5,3] <- ifelse(het_conurbano[3,4]<0.1,paste0(het_conurbano[3,2],"*"),het_conurbano[3,2])
COR_het_region[5,3] <- ifelse(het_conurbano[3,4]<0.05,paste0(het_conurbano[3,2],"**"),COR_het_region[5,3])
COR_het_region[5,3] <- ifelse(het_conurbano[3,4]<0.01,paste0(het_conurbano[3,2],"***"),COR_het_region[5,3])

COR_het_region[7,3] <- ifelse(het_conurbano[4,4]<0.1,paste0(het_conurbano[4,2],"*"),het_conurbano[4,2])
COR_het_region[7,3] <- ifelse(het_conurbano[4,4]<0.05,paste0(het_conurbano[4,2],"**"),COR_het_region[7,3])
COR_het_region[7,3] <- ifelse(het_conurbano[4,4]<0.01,paste0(het_conurbano[4,2],"***"),COR_het_region[7,3])

COR_het_region[9,3] <- ifelse(het_conurbano[5,4]<0.1,paste0(het_conurbano[5,2],"*"),het_conurbano[5,2])
COR_het_region[9,3] <- ifelse(het_conurbano[5,4]<0.05,paste0(het_conurbano[5,2],"**"),COR_het_region[9,3])
COR_het_region[9,3] <- ifelse(het_conurbano[5,4]<0.01,paste0(het_conurbano[5,2],"***"),COR_het_region[9,3])

COR_het_region[11,3] <- ifelse(het_conurbano[6,4]<0.1,paste0(het_conurbano[6,2],"*"),het_conurbano[6,2])
COR_het_region[11,3] <- ifelse(het_conurbano[6,4]<0.05,paste0(het_conurbano[6,2],"**"),COR_het_region[11,3])
COR_het_region[11,3] <- ifelse(het_conurbano[6,4]<0.01,paste0(het_conurbano[6,2],"***"),COR_het_region[11,3])

COR_het_region[2,3] <- paste0("(",het_conurbano[1,3],")")
COR_het_region[4,3] <- paste0("(",het_conurbano[2,3],")")
COR_het_region[6,3] <- paste0("(",het_conurbano[3,3],")")
COR_het_region[8,3] <- paste0("(",het_conurbano[4,3],")")
COR_het_region[10,3] <- paste0("(",het_conurbano[5,3],")")
COR_het_region[12,3] <- paste0("(",het_conurbano[6,3],")")

het_interact_region[,2] <- as.numeric(as.character(het_interact_region[,2]))
het_interact_region[,3] <- as.numeric(as.character(het_interact_region[,3]))
het_interact_region[,4] <- as.numeric(as.character(het_interact_region[,4]))

COR_het_region[13,4] <- "Interaction Term"
COR_het_region[1,4] <- ifelse(het_interact_region[1,4]<0.1,paste0(het_interact_region[1,2],"*"),het_interact_region[1,2])
COR_het_region[1,4] <- ifelse(het_interact_region[1,4]<0.05,paste0(het_interact_region[1,2],"**"),COR_het_region[1,4])
COR_het_region[1,4] <- ifelse(het_interact_region[1,4]<0.01,paste0(het_interact_region[1,2],"***"),COR_het_region[1,4])

COR_het_region[3,4] <- ifelse(het_interact_region[2,4]<0.1,paste0(het_interact_region[2,2],"*"),het_interact_region[2,2])
COR_het_region[3,4] <- ifelse(het_interact_region[2,4]<0.05,paste0(het_interact_region[2,2],"**"),COR_het_region[3,4])
COR_het_region[3,4] <- ifelse(het_interact_region[2,4]<0.01,paste0(het_interact_region[2,2],"***"),COR_het_region[3,4])

COR_het_region[5,4] <- ifelse(het_interact_region[3,4]<0.1,paste0(het_interact_region[3,2],"*"),het_interact_region[3,2])
COR_het_region[5,4] <- ifelse(het_interact_region[3,4]<0.05,paste0(het_interact_region[3,2],"**"),COR_het_region[5,4])
COR_het_region[5,4] <- ifelse(het_interact_region[3,4]<0.01,paste0(het_interact_region[3,2],"***"),COR_het_region[5,4])

COR_het_region[7,4] <- ifelse(het_interact_region[4,4]<0.1,paste0(het_interact_region[4,2],"*"),het_interact_region[4,2])
COR_het_region[7,4] <- ifelse(het_interact_region[4,4]<0.05,paste0(het_interact_region[4,2],"**"),COR_het_region[7,4])
COR_het_region[7,4] <- ifelse(het_interact_region[4,4]<0.01,paste0(het_interact_region[4,2],"***"),COR_het_region[7,4])

COR_het_region[9,4] <- ifelse(het_interact_region[5,4]<0.1,paste0(het_interact_region[5,2],"*"),het_interact_region[5,2])
COR_het_region[9,4] <- ifelse(het_interact_region[5,4]<0.05,paste0(het_interact_region[5,2],"**"),COR_het_region[9,4])
COR_het_region[9,4] <- ifelse(het_interact_region[5,4]<0.01,paste0(het_interact_region[5,2],"***"),COR_het_region[9,4])

COR_het_region[11,4] <- ifelse(het_interact_region[6,4]<0.1,paste0(het_interact_region[6,2],"*"),het_interact_region[6,2])
COR_het_region[11,4] <- ifelse(het_interact_region[6,4]<0.05,paste0(het_interact_region[6,2],"**"),COR_het_region[11,4])
COR_het_region[11,4] <- ifelse(het_interact_region[6,4]<0.01,paste0(het_interact_region[6,2],"***"),COR_het_region[11,4])

COR_het_region[2,4] <- paste0("(",het_interact_region[1,3],")")
COR_het_region[4,4] <- paste0("(",het_interact_region[2,3],")")
COR_het_region[6,4] <- paste0("(",het_interact_region[3,3],")")
COR_het_region[8,4] <- paste0("(",het_interact_region[4,3],")")
COR_het_region[10,4] <- paste0("(",het_interact_region[5,3],")")
COR_het_region[12,4] <- paste0("(",het_interact_region[6,3],")")

COR_het_region

#Table A17:
stargazer(as.matrix(COR_het_region), rownames=F, out="TableA17.tex")


###by age:
###Table A20:

##age_1:
age_1 <- subset(COR, COR$age<41) #below median age:

outcomes_age_1 <- rbind(
  c("progcorrupt",
    summary(lm(progcorrupt ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=age_1))$coefficients[1,1],
    summary(lm(progcorrupt ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=age_1))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(age_1,age_1$treat14==0|age_1$treat14==1)$progcorrupt),1,0))),
  c("participation_willingness",
    summary(lm(participation_willingness ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=age_1))$coefficients[1,1],
    summary(lm(participation_willingness ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=age_1))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(age_1,age_1$treat14==0|age_1$treat14==1)$participation_willingness),1,0))),
  c("vote_invalid",
    summary(lm(vote_invalid ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=age_1))$coefficients[1,1],
    summary(lm(vote_invalid ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=age_1))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(age_1,age_1$treat14==0|age_1$treat14==1)$vote_invalid),1,0))),
  c("conf_judge",
    summary(lm(conf_judge ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=age_1))$coefficients[1,1],
    summary(lm(conf_judge ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=age_1))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(age_1,age_1$treat14==0|age_1$treat14==1)$conf_judge),1,0))),
  c("conf_parties",
    summary(lm(conf_parties ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=age_1))$coefficients[1,1],
    summary(lm(conf_parties ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=age_1))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(age_1,age_1$treat14==0|age_1$treat14==1)$conf_parties),1,0))),
  c("conf_parl",
    summary(lm(conf_parl ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=age_1))$coefficients[1,1],
    summary(lm(conf_parl ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=age_1))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(age_1,age_1$treat14==0|age_1$treat14==1)$conf_parl),1,0)))
)

colnames(outcomes_age_1) <- c("Variable",
                              "Intercept14","Estimate14","SE14","pvalue14","ValidN14")

het_age_1 <- outcomes_age_1[,c(1,3,4:6)]
het_age_1[,2] <- round(as.numeric(het_age_1[,2]),3)
het_age_1[,3] <- round(as.numeric(het_age_1[,3]),3)
het_age_1[,4] <- round(as.numeric(het_age_1[,4]),3) 

het_age_1 <- as.data.frame(het_age_1)

het_age_1[,1] <- c("Progress on corruption","Inclination to vote or protest","Invalid vote","Trust in judiciary","Trust in parties","Trust in congress")

colnames(het_age_1) <- c("Variable","Effect","SE","p Value", "N")

het_age_1


##age_2:
age_2 <- subset(COR, COR$age>40)

outcomes_age_2 <- rbind(
  c("progcorrupt",
    summary(lm(progcorrupt ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=age_2))$coefficients[1,1],
    summary(lm(progcorrupt ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=age_2))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(age_2,age_2$treat14==0|age_2$treat14==1)$progcorrupt),1,0))),
  c("participation_willingness",
    summary(lm(participation_willingness ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=age_2))$coefficients[1,1],
    summary(lm(participation_willingness ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=age_2))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(age_2,age_2$treat14==0|age_2$treat14==1)$participation_willingness),1,0))),
  c("vote_invalid",
    summary(lm(vote_invalid ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=age_2))$coefficients[1,1],
    summary(lm(vote_invalid ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=age_2))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(age_2,age_2$treat14==0|age_2$treat14==1)$vote_invalid),1,0))),
  c("conf_judge",
    summary(lm(conf_judge ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=age_2))$coefficients[1,1],
    summary(lm(conf_judge ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=age_2))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(age_2,age_2$treat14==0|age_2$treat14==1)$conf_judge),1,0))),
  c("conf_parties",
    summary(lm(conf_parties ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=age_2))$coefficients[1,1],
    summary(lm(conf_parties ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=age_2))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(age_2,age_2$treat14==0|age_2$treat14==1)$conf_parties),1,0))),
  c("conf_parl",
    summary(lm(conf_parl ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=age_2))$coefficients[1,1],
    summary(lm(conf_parl ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=age_2))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(age_2,age_2$treat14==0|age_2$treat14==1)$conf_parl),1,0)))
)

colnames(outcomes_age_2) <- c("Variable",
                              "Intercept14","Estimate14","SE14","pvalue14","ValidN14")

het_age_2 <- outcomes_age_2[,c(1,3,4:6)]
het_age_2[,2] <- round(as.numeric(het_age_2[,2]),3)
het_age_2[,3] <- round(as.numeric(het_age_2[,3]),3)
het_age_2[,4] <- round(as.numeric(het_age_2[,4]),3) 

het_age_2 <- as.data.frame(het_age_2)

het_age_2[,1] <- c("Progress on corruption","Inclination to vote or protest","Invalid vote","Trust in judiciary","Trust in parties","Trust in congress")

colnames(het_age_2) <- c("Variable","Effect","SE","p Value", "N")

het_age_2


##interact_age:
valid_age <- subset(COR, !is.na(age))

outcomes_interact_age <- rbind(
  c("progcorrupt",
    summary(lm(progcorrupt ~ treat14 * age + treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=valid_age))$coefficients[1,1],
    summary(lm(progcorrupt ~ treat14 * age + treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=valid_age))$coefficients[32,][c(1,2,4)],
    sum(ifelse(!is.na(subset(valid_age,valid_age$treat14==0|valid_age$treat14==1)$progcorrupt),1,0))),
  c("participation_willingness",
    summary(lm(participation_willingness ~ treat14 * age + treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=valid_age))$coefficients[1,1],
    summary(lm(participation_willingness ~ treat14 * age + treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=valid_age))$coefficients[32,][c(1,2,4)],
    sum(ifelse(!is.na(subset(valid_age,valid_age$treat14==0|valid_age$treat14==1)$participation_willingness),1,0))),
  c("vote_invalid",
    summary(lm(vote_invalid ~ treat14 * age + treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=valid_age))$coefficients[1,1],
    summary(lm(vote_invalid ~ treat14 * age + treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=valid_age))$coefficients[32,][c(1,2,4)],
    sum(ifelse(!is.na(subset(valid_age,valid_age$treat14==0|valid_age$treat14==1)$vote_invalid),1,0))),
  c("conf_judge",
    summary(lm(conf_judge ~ treat14 * age + treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=valid_age))$coefficients[1,1],
    summary(lm(conf_judge ~ treat14 * age + treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=valid_age))$coefficients[32,][c(1,2,4)],
    sum(ifelse(!is.na(subset(valid_age,valid_age$treat14==0|valid_age$treat14==1)$conf_judge),1,0))),
  c("conf_parties",
    summary(lm(conf_parties ~ treat14 * age + treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=valid_age))$coefficients[1,1],
    summary(lm(conf_parties ~ treat14 * age + treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=valid_age))$coefficients[32,][c(1,2,4)],
    sum(ifelse(!is.na(subset(valid_age,valid_age$treat14==0|valid_age$treat14==1)$conf_parties),1,0))),
  c("conf_parl",
    summary(lm(conf_parl ~ treat14 * age + treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=valid_age))$coefficients[1,1],
    summary(lm(conf_parl ~ treat14 * age + treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=valid_age))$coefficients[32,][c(1,2,4)],
    sum(ifelse(!is.na(subset(valid_age,valid_age$treat14==0|valid_age$treat14==1)$conf_parl),1,0)))
)

colnames(outcomes_interact_age) <- c("Variable",
                                     "Intercept14","Estimate14","SE14","pvalue14","ValidN14")

het_interact_age <- outcomes_interact_age[,c(1,3,4:6)]
het_interact_age[,2] <- round(as.numeric(het_interact_age[,2]),3)
het_interact_age[,3] <- round(as.numeric(het_interact_age[,3]),3)
het_interact_age[,4] <- round(as.numeric(het_interact_age[,4]),3) 

het_interact_age <- as.data.frame(het_interact_age)

het_interact_age[,1] <-  c("Progress on corruption","Inclination to vote or protest","Invalid vote","Trust in judiciary","Trust in parties","Trust in congress")

colnames(het_interact_age) <- c("Variable","Effect","SE","p Value", "N")

het_interact_age

##combine the output for the different subgroup analyses & interaction term:
COR_het_age <- as.data.frame(cbind(rep(NA,2*nrow(het_age_1)),rep(NA,2*nrow(het_age_1)),rep(NA,2*nrow(het_age_1)),rep(NA,2*nrow(het_age_1))))

COR_het_age[1,1] <- het_age_1[1,1]
COR_het_age[2,1] <- ""
COR_het_age[3,1] <- het_age_1[2,1]
COR_het_age[4,1] <- ""
COR_het_age[5,1] <- het_age_1[3,1]
COR_het_age[6,1] <- ""
COR_het_age[7,1] <- het_age_1[4,1]
COR_het_age[8,1] <- ""
COR_het_age[9,1] <- het_age_1[5,1]
COR_het_age[10,1] <- ""
COR_het_age[11,1] <- het_age_1[6,1]
COR_het_age[12,1] <- ""
COR_het_age[13,1] <- "Sample"

het_age_1[,2] <- as.numeric(as.character(het_age_1[,2]))
het_age_1[,3] <- as.numeric(as.character(het_age_1[,3]))
het_age_1[,4] <- as.numeric(as.character(het_age_1[,4]))

COR_het_age[13,2] <- "Young"
COR_het_age[1,2] <- ifelse(het_age_1[1,4]<0.1,paste0(het_age_1[1,2],"*"),het_age_1[1,2])
COR_het_age[1,2] <- ifelse(het_age_1[1,4]<0.05,paste0(het_age_1[1,2],"**"),COR_het_age[1,2])
COR_het_age[1,2] <- ifelse(het_age_1[1,4]<0.01,paste0(het_age_1[1,2],"***"),COR_het_age[1,2])

COR_het_age[3,2] <- ifelse(het_age_1[2,4]<0.1,paste0(het_age_1[2,2],"*"),het_age_1[2,2])
COR_het_age[3,2] <- ifelse(het_age_1[2,4]<0.05,paste0(het_age_1[2,2],"**"),COR_het_age[3,2])
COR_het_age[3,2] <- ifelse(het_age_1[2,4]<0.01,paste0(het_age_1[2,2],"***"),COR_het_age[3,2])

COR_het_age[5,2] <- ifelse(het_age_1[3,4]<0.1,paste0(het_age_1[3,2],"*"),het_age_1[3,2])
COR_het_age[5,2] <- ifelse(het_age_1[3,4]<0.05,paste0(het_age_1[3,2],"**"),COR_het_age[5,2])
COR_het_age[5,2] <- ifelse(het_age_1[3,4]<0.01,paste0(het_age_1[3,2],"***"),COR_het_age[5,2])

COR_het_age[7,2] <- ifelse(het_age_1[4,4]<0.1,paste0(het_age_1[4,2],"*"),het_age_1[4,2])
COR_het_age[7,2] <- ifelse(het_age_1[4,4]<0.05,paste0(het_age_1[4,2],"**"),COR_het_age[7,2])
COR_het_age[7,2] <- ifelse(het_age_1[4,4]<0.01,paste0(het_age_1[4,2],"***"),COR_het_age[7,2])

COR_het_age[9,2] <- ifelse(het_age_1[5,4]<0.1,paste0(het_age_1[5,2],"*"),het_age_1[5,2])
COR_het_age[9,2] <- ifelse(het_age_1[5,4]<0.05,paste0(het_age_1[5,2],"**"),COR_het_age[9,2])
COR_het_age[9,2] <- ifelse(het_age_1[5,4]<0.01,paste0(het_age_1[5,2],"***"),COR_het_age[9,2])

COR_het_age[11,2] <- ifelse(het_age_1[6,4]<0.1,paste0(het_age_1[6,2],"*"),het_age_1[6,2])
COR_het_age[11,2] <- ifelse(het_age_1[6,4]<0.05,paste0(het_age_1[6,2],"**"),COR_het_age[11,2])
COR_het_age[11,2] <- ifelse(het_age_1[6,4]<0.01,paste0(het_age_1[6,2],"***"),COR_het_age[11,2])

COR_het_age[2,2] <- paste0("(",het_age_1[1,3],")")
COR_het_age[4,2] <- paste0("(",het_age_1[2,3],")")
COR_het_age[6,2] <- paste0("(",het_age_1[3,3],")")
COR_het_age[8,2] <- paste0("(",het_age_1[4,3],")")
COR_het_age[10,2] <- paste0("(",het_age_1[5,3],")")
COR_het_age[12,2] <- paste0("(",het_age_1[6,3],")")

het_age_2[,2] <- as.numeric(as.character(het_age_2[,2]))
het_age_2[,3] <- as.numeric(as.character(het_age_2[,3]))
het_age_2[,4] <- as.numeric(as.character(het_age_2[,4]))

COR_het_age[13,3] <- "Old"
COR_het_age[1,3] <- ifelse(het_age_2[1,4]<0.1,paste0(het_age_2[1,2],"*"),het_age_2[1,2])
COR_het_age[1,3] <- ifelse(het_age_2[1,4]<0.05,paste0(het_age_2[1,2],"**"),COR_het_age[1,3])
COR_het_age[1,3] <- ifelse(het_age_2[1,4]<0.01,paste0(het_age_2[1,2],"***"),COR_het_age[1,3])

COR_het_age[3,3] <- ifelse(het_age_2[2,4]<0.1,paste0(het_age_2[2,2],"*"),het_age_2[2,2])
COR_het_age[3,3] <- ifelse(het_age_2[2,4]<0.05,paste0(het_age_2[2,2],"**"),COR_het_age[3,3])
COR_het_age[3,3] <- ifelse(het_age_2[2,4]<0.01,paste0(het_age_2[2,2],"***"),COR_het_age[3,3])

COR_het_age[5,3] <- ifelse(het_age_2[3,4]<0.1,paste0(het_age_2[3,2],"*"),het_age_2[3,2])
COR_het_age[5,3] <- ifelse(het_age_2[3,4]<0.05,paste0(het_age_2[3,2],"**"),COR_het_age[5,3])
COR_het_age[5,3] <- ifelse(het_age_2[3,4]<0.01,paste0(het_age_2[3,2],"***"),COR_het_age[5,3])

COR_het_age[7,3] <- ifelse(het_age_2[4,4]<0.1,paste0(het_age_2[4,2],"*"),het_age_2[4,2])
COR_het_age[7,3] <- ifelse(het_age_2[4,4]<0.05,paste0(het_age_2[4,2],"**"),COR_het_age[7,3])
COR_het_age[7,3] <- ifelse(het_age_2[4,4]<0.01,paste0(het_age_2[4,2],"***"),COR_het_age[7,3])

COR_het_age[9,3] <- ifelse(het_age_2[5,4]<0.1,paste0(het_age_2[5,2],"*"),het_age_2[5,2])
COR_het_age[9,3] <- ifelse(het_age_2[5,4]<0.05,paste0(het_age_2[5,2],"**"),COR_het_age[9,3])
COR_het_age[9,3] <- ifelse(het_age_2[5,4]<0.01,paste0(het_age_2[5,2],"***"),COR_het_age[9,3])

COR_het_age[11,3] <- ifelse(het_age_2[6,4]<0.1,paste0(het_age_2[6,2],"*"),het_age_2[6,2])
COR_het_age[11,3] <- ifelse(het_age_2[6,4]<0.05,paste0(het_age_2[6,2],"**"),COR_het_age[11,3])
COR_het_age[11,3] <- ifelse(het_age_2[6,4]<0.01,paste0(het_age_2[6,2],"***"),COR_het_age[11,3])

COR_het_age[2,3] <- paste0("(",het_age_2[1,3],")")
COR_het_age[4,3] <- paste0("(",het_age_2[2,3],")")
COR_het_age[6,3] <- paste0("(",het_age_2[3,3],")")
COR_het_age[8,3] <- paste0("(",het_age_2[4,3],")")
COR_het_age[10,3] <- paste0("(",het_age_2[5,3],")")
COR_het_age[12,3] <- paste0("(",het_age_2[6,3],")")

het_interact_age[,2] <- as.numeric(as.character(het_interact_age[,2]))
het_interact_age[,3] <- as.numeric(as.character(het_interact_age[,3]))
het_interact_age[,4] <- as.numeric(as.character(het_interact_age[,4]))

COR_het_age[13,4] <- "Interaction Term"
COR_het_age[1,4] <- ifelse(het_interact_age[1,4]<0.1,paste0(het_interact_age[1,2],"*"),het_interact_age[1,2])
COR_het_age[1,4] <- ifelse(het_interact_age[1,4]<0.05,paste0(het_interact_age[1,2],"**"),COR_het_age[1,4])
COR_het_age[1,4] <- ifelse(het_interact_age[1,4]<0.01,paste0(het_interact_age[1,2],"***"),COR_het_age[1,4])

COR_het_age[3,4] <- ifelse(het_interact_age[2,4]<0.1,paste0(het_interact_age[2,2],"*"),het_interact_age[2,2])
COR_het_age[3,4] <- ifelse(het_interact_age[2,4]<0.05,paste0(het_interact_age[2,2],"**"),COR_het_age[3,4])
COR_het_age[3,4] <- ifelse(het_interact_age[2,4]<0.01,paste0(het_interact_age[2,2],"***"),COR_het_age[3,4])

COR_het_age[5,4] <- ifelse(het_interact_age[3,4]<0.1,paste0(het_interact_age[3,2],"*"),het_interact_age[3,2])
COR_het_age[5,4] <- ifelse(het_interact_age[3,4]<0.05,paste0(het_interact_age[3,2],"**"),COR_het_age[5,4])
COR_het_age[5,4] <- ifelse(het_interact_age[3,4]<0.01,paste0(het_interact_age[3,2],"***"),COR_het_age[5,4])

COR_het_age[7,4] <- ifelse(het_interact_age[4,4]<0.1,paste0(het_interact_age[4,2],"*"),het_interact_age[4,2])
COR_het_age[7,4] <- ifelse(het_interact_age[4,4]<0.05,paste0(het_interact_age[4,2],"**"),COR_het_age[7,4])
COR_het_age[7,4] <- ifelse(het_interact_age[4,4]<0.01,paste0(het_interact_age[4,2],"***"),COR_het_age[7,4])

COR_het_age[9,4] <- ifelse(het_interact_age[5,4]<0.1,paste0(het_interact_age[5,2],"*"),het_interact_age[5,2])
COR_het_age[9,4] <- ifelse(het_interact_age[5,4]<0.05,paste0(het_interact_age[5,2],"**"),COR_het_age[9,4])
COR_het_age[9,4] <- ifelse(het_interact_age[5,4]<0.01,paste0(het_interact_age[5,2],"***"),COR_het_age[9,4])

COR_het_age[11,4] <- ifelse(het_interact_age[6,4]<0.1,paste0(het_interact_age[6,2],"*"),het_interact_age[6,2])
COR_het_age[11,4] <- ifelse(het_interact_age[6,4]<0.05,paste0(het_interact_age[6,2],"**"),COR_het_age[11,4])
COR_het_age[11,4] <- ifelse(het_interact_age[6,4]<0.01,paste0(het_interact_age[6,2],"***"),COR_het_age[11,4])

COR_het_age[2,4] <- paste0("(",het_interact_age[1,3],")")
COR_het_age[4,4] <- paste0("(",het_interact_age[2,3],")")
COR_het_age[6,4] <- paste0("(",het_interact_age[3,3],")")
COR_het_age[8,4] <- paste0("(",het_interact_age[4,3],")")
COR_het_age[10,4] <- paste0("(",het_interact_age[5,3],")")
COR_het_age[12,4] <- paste0("(",het_interact_age[6,3],")")

COR_het_age

#Table A20:
stargazer(as.matrix(COR_het_age), rownames=F, out="TableA20.tex")


###by past voting status:
###Table A19:

##voted_not_past:
voted_not_past <- subset(COR, COR$voted==0)

outcomes_voted_not_past <- rbind(
  c("progcorrupt",
    summary(lm(progcorrupt ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=voted_not_past))$coefficients[1,1],
    summary(lm(progcorrupt ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=voted_not_past))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(voted_not_past,voted_not_past$treat14==0|voted_not_past$treat14==1)$progcorrupt),1,0))),
  c("participation_willingness",
    summary(lm(participation_willingness ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=voted_not_past))$coefficients[1,1],
    summary(lm(participation_willingness ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=voted_not_past))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(voted_not_past,voted_not_past$treat14==0|voted_not_past$treat14==1)$participation_willingness),1,0))),
  c("vote_invalid",
    summary(lm(vote_invalid ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=voted_not_past))$coefficients[1,1],
    summary(lm(vote_invalid ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=voted_not_past))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(voted_not_past,voted_not_past$treat14==0|voted_not_past$treat14==1)$vote_invalid),1,0))),
  c("conf_judge",
    summary(lm(conf_judge ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=voted_not_past))$coefficients[1,1],
    summary(lm(conf_judge ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=voted_not_past))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(voted_not_past,voted_not_past$treat14==0|voted_not_past$treat14==1)$conf_judge),1,0))),
  c("conf_parties",
    summary(lm(conf_parties ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=voted_not_past))$coefficients[1,1],
    summary(lm(conf_parties ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=voted_not_past))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(voted_not_past,voted_not_past$treat14==0|voted_not_past$treat14==1)$conf_parties),1,0))),
  c("conf_parl",
    summary(lm(conf_parl ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=voted_not_past))$coefficients[1,1],
    summary(lm(conf_parl ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=voted_not_past))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(voted_not_past,voted_not_past$treat14==0|voted_not_past$treat14==1)$conf_parl),1,0)))
)

colnames(outcomes_voted_not_past) <- c("Variable",
"Intercept14","Estimate14","SE14","pvalue14","ValidN14")

het_voted_not_past <- outcomes_voted_not_past[,c(1,3,4:6)]
het_voted_not_past[,2] <- round(as.numeric(het_voted_not_past[,2]),3)
het_voted_not_past[,3] <- round(as.numeric(het_voted_not_past[,3]),3)
het_voted_not_past[,4] <- round(as.numeric(het_voted_not_past[,4]),3) 

het_voted_not_past <- as.data.frame(het_voted_not_past)

het_voted_not_past[,1] <- c("Progress on corruption","Inclination to vote or protest","Invalid vote","Trust in judiciary","Trust in parties","Trust in congress")

colnames(het_voted_not_past) <- c("Variable","Effect","SE","p Value", "N")

het_voted_not_past


##voted_past:
voted_past <- subset(COR, COR$voted==1)

outcomes_voted_past <- rbind(
  c("progcorrupt",
    summary(lm(progcorrupt ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=voted_past))$coefficients[1,1],
    summary(lm(progcorrupt ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=voted_past))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(voted_past,voted_past$treat14==0|voted_past$treat14==1)$progcorrupt),1,0))),
  c("participation_willingness",
    summary(lm(participation_willingness ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=voted_past))$coefficients[1,1],
    summary(lm(participation_willingness ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=voted_past))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(voted_past,voted_past$treat14==0|voted_past$treat14==1)$participation_willingness),1,0))),
  c("vote_invalid",
    summary(lm(vote_invalid ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=voted_past))$coefficients[1,1],
    summary(lm(vote_invalid ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=voted_past))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(voted_past,voted_past$treat14==0|voted_past$treat14==1)$vote_invalid),1,0))),
  c("conf_judge",
    summary(lm(conf_judge ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=voted_past))$coefficients[1,1],
    summary(lm(conf_judge ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=voted_past))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(voted_past,voted_past$treat14==0|voted_past$treat14==1)$conf_judge),1,0))),
  c("conf_parties",
    summary(lm(conf_parties ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=voted_past))$coefficients[1,1],
    summary(lm(conf_parties ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=voted_past))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(voted_past,voted_past$treat14==0|voted_past$treat14==1)$conf_parties),1,0))),
  c("conf_parl",
    summary(lm(conf_parl ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=voted_past))$coefficients[1,1],
    summary(lm(conf_parl ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=voted_past))$coefficients[2,][c(1,2,4)],
    sum(ifelse(!is.na(subset(voted_past,voted_past$treat14==0|voted_past$treat14==1)$conf_parl),1,0)))
)

colnames(outcomes_voted_past) <- c("Variable",
                                   "Intercept14","Estimate14","SE14","pvalue14","ValidN14")

het_voted_past <- outcomes_voted_past[,c(1,3,4:6)]
het_voted_past[,2] <- round(as.numeric(het_voted_past[,2]),3)
het_voted_past[,3] <- round(as.numeric(het_voted_past[,3]),3)
het_voted_past[,4] <- round(as.numeric(het_voted_past[,4]),3) 

het_voted_past <- as.data.frame(het_voted_past)

het_voted_past[,1] <-  c("Progress on corruption","Inclination to vote or protest","Invalid vote","Trust in judiciary","Trust in parties","Trust in congress")

colnames(het_voted_past) <- c("Variable","Effect","SE","p Value", "N")

het_voted_past


##interact_voted_past:
valid_voted_past <- subset(COR, !is.na(voted))

outcomes_interact_voted_past <- rbind(
  c("progcorrupt",
    summary(lm(progcorrupt ~ treat14 * voted + treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=valid_voted_past))$coefficients[1,1],
    summary(lm(progcorrupt ~ treat14 * voted + treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=valid_voted_past))$coefficients[32,][c(1,2,4)],
    sum(ifelse(!is.na(subset(valid_voted_past,valid_voted_past$treat14==0|valid_voted_past$treat14==1)$progcorrupt),1,0))),
  c("participation_willingness",
    summary(lm(participation_willingness ~ treat14 * voted + treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=valid_voted_past))$coefficients[1,1],
    summary(lm(participation_willingness ~ treat14 * voted + treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=valid_voted_past))$coefficients[32,][c(1,2,4)],
    sum(ifelse(!is.na(subset(valid_voted_past,valid_voted_past$treat14==0|valid_voted_past$treat14==1)$participation_willingness),1,0))),
  c("vote_invalid",
    summary(lm(vote_invalid ~ treat14 * voted + treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=valid_voted_past))$coefficients[1,1],
    summary(lm(vote_invalid ~ treat14 * voted + treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=valid_voted_past))$coefficients[32,][c(1,2,4)],
    sum(ifelse(!is.na(subset(valid_voted_past,valid_voted_past$treat14==0|valid_voted_past$treat14==1)$vote_invalid),1,0))),
  c("conf_judge",
    summary(lm(conf_judge ~ treat14 * voted + treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=valid_voted_past))$coefficients[1,1],
    summary(lm(conf_judge ~ treat14 * voted + treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=valid_voted_past))$coefficients[32,][c(1,2,4)],
    sum(ifelse(!is.na(subset(valid_voted_past,valid_voted_past$treat14==0|valid_voted_past$treat14==1)$conf_judge),1,0))),
  c("conf_parties",
    summary(lm(conf_parties ~ treat14 * voted + treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=valid_voted_past))$coefficients[1,1],
    summary(lm(conf_parties ~ treat14 * voted + treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=valid_voted_past))$coefficients[32,][c(1,2,4)],
    sum(ifelse(!is.na(subset(valid_voted_past,valid_voted_past$treat14==0|valid_voted_past$treat14==1)$conf_parties),1,0))),
  c("conf_parl",
    summary(lm(conf_parl ~ treat14 * voted + treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=valid_voted_past))$coefficients[1,1],
    summary(lm(conf_parl ~ treat14 * voted + treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=valid_voted_past))$coefficients[32,][c(1,2,4)],
    sum(ifelse(!is.na(subset(valid_voted_past,valid_voted_past$treat14==0|valid_voted_past$treat14==1)$conf_parl),1,0)))
)

colnames(outcomes_interact_voted_past) <- c("Variable",
                                            "Intercept14","Estimate14","SE14","pvalue14","ValidN14")

het_interact_voted_past <- outcomes_interact_voted_past[,c(1,3,4:6)]
het_interact_voted_past[,2] <- round(as.numeric(het_interact_voted_past[,2]),3)
het_interact_voted_past[,3] <- round(as.numeric(het_interact_voted_past[,3]),3)
het_interact_voted_past[,4] <- round(as.numeric(het_interact_voted_past[,4]),3) 

het_interact_voted_past <- as.data.frame(het_interact_voted_past)

het_interact_voted_past[,1] <-  c("Progress on corruption","Inclination to vote or protest","Invalid vote","Trust in judiciary","Trust in parties","Trust in congress")

colnames(het_interact_voted_past) <- c("Variable","Effect","SE","p Value", "N")

het_interact_voted_past

##combine the output for the different subgroup analyses & interaction term:
COR_het_voted_past <- as.data.frame(cbind(rep(NA,2*nrow(het_voted_not_past)),rep(NA,2*nrow(het_voted_not_past)),rep(NA,2*nrow(het_voted_not_past)),rep(NA,2*nrow(het_voted_not_past))))

COR_het_voted_past[1,1] <- het_voted_not_past[1,1]
COR_het_voted_past[2,1] <- ""
COR_het_voted_past[3,1] <- het_voted_not_past[2,1]
COR_het_voted_past[4,1] <- ""
COR_het_voted_past[5,1] <- het_voted_not_past[3,1]
COR_het_voted_past[6,1] <- ""
COR_het_voted_past[7,1] <- het_voted_not_past[4,1]
COR_het_voted_past[8,1] <- ""
COR_het_voted_past[9,1] <- het_voted_not_past[5,1]
COR_het_voted_past[10,1] <- ""
COR_het_voted_past[11,1] <- het_voted_not_past[6,1]
COR_het_voted_past[12,1] <- ""
COR_het_voted_past[13,1] <- "Sample"

het_voted_not_past[,2] <- as.numeric(as.character(het_voted_not_past[,2]))
het_voted_not_past[,3] <- as.numeric(as.character(het_voted_not_past[,3]))
het_voted_not_past[,4] <- as.numeric(as.character(het_voted_not_past[,4]))

COR_het_voted_past[13,2] <- "Previous Non Voters"
COR_het_voted_past[1,2] <- ifelse(het_voted_not_past[1,4]<0.1,paste0(het_voted_not_past[1,2],"*"),het_voted_not_past[1,2])
COR_het_voted_past[1,2] <- ifelse(het_voted_not_past[1,4]<0.05,paste0(het_voted_not_past[1,2],"**"),COR_het_voted_past[1,2])
COR_het_voted_past[1,2] <- ifelse(het_voted_not_past[1,4]<0.01,paste0(het_voted_not_past[1,2],"***"),COR_het_voted_past[1,2])

COR_het_voted_past[3,2] <- ifelse(het_voted_not_past[2,4]<0.1,paste0(het_voted_not_past[2,2],"*"),het_voted_not_past[2,2])
COR_het_voted_past[3,2] <- ifelse(het_voted_not_past[2,4]<0.05,paste0(het_voted_not_past[2,2],"**"),COR_het_voted_past[3,2])
COR_het_voted_past[3,2] <- ifelse(het_voted_not_past[2,4]<0.01,paste0(het_voted_not_past[2,2],"***"),COR_het_voted_past[3,2])

COR_het_voted_past[5,2] <- ifelse(het_voted_not_past[3,4]<0.1,paste0(het_voted_not_past[3,2],"*"),het_voted_not_past[3,2])
COR_het_voted_past[5,2] <- ifelse(het_voted_not_past[3,4]<0.05,paste0(het_voted_not_past[3,2],"**"),COR_het_voted_past[5,2])
COR_het_voted_past[5,2] <- ifelse(het_voted_not_past[3,4]<0.01,paste0(het_voted_not_past[3,2],"***"),COR_het_voted_past[5,2])

COR_het_voted_past[7,2] <- ifelse(het_voted_not_past[4,4]<0.1,paste0(het_voted_not_past[4,2],"*"),het_voted_not_past[4,2])
COR_het_voted_past[7,2] <- ifelse(het_voted_not_past[4,4]<0.05,paste0(het_voted_not_past[4,2],"**"),COR_het_voted_past[7,2])
COR_het_voted_past[7,2] <- ifelse(het_voted_not_past[4,4]<0.01,paste0(het_voted_not_past[4,2],"***"),COR_het_voted_past[7,2])

COR_het_voted_past[9,2] <- ifelse(het_voted_not_past[5,4]<0.1,paste0(het_voted_not_past[5,2],"*"),het_voted_not_past[5,2])
COR_het_voted_past[9,2] <- ifelse(het_voted_not_past[5,4]<0.05,paste0(het_voted_not_past[5,2],"**"),COR_het_voted_past[9,2])
COR_het_voted_past[9,2] <- ifelse(het_voted_not_past[5,4]<0.01,paste0(het_voted_not_past[5,2],"***"),COR_het_voted_past[9,2])

COR_het_voted_past[11,2] <- ifelse(het_voted_not_past[6,4]<0.1,paste0(het_voted_not_past[6,2],"*"),het_voted_not_past[6,2])
COR_het_voted_past[11,2] <- ifelse(het_voted_not_past[6,4]<0.05,paste0(het_voted_not_past[6,2],"**"),COR_het_voted_past[11,2])
COR_het_voted_past[11,2] <- ifelse(het_voted_not_past[6,4]<0.01,paste0(het_voted_not_past[6,2],"***"),COR_het_voted_past[11,2])

COR_het_voted_past[2,2] <- paste0("(",het_voted_not_past[1,3],")")
COR_het_voted_past[4,2] <- paste0("(",het_voted_not_past[2,3],")")
COR_het_voted_past[6,2] <- paste0("(",het_voted_not_past[3,3],")")
COR_het_voted_past[8,2] <- paste0("(",het_voted_not_past[4,3],")")
COR_het_voted_past[10,2] <- paste0("(",het_voted_not_past[5,3],")")
COR_het_voted_past[12,2] <- paste0("(",het_voted_not_past[6,3],")")

het_voted_past[,2] <- as.numeric(as.character(het_voted_past[,2]))
het_voted_past[,3] <- as.numeric(as.character(het_voted_past[,3]))
het_voted_past[,4] <- as.numeric(as.character(het_voted_past[,4]))

COR_het_voted_past[13,3] <- "Previous Voters"
COR_het_voted_past[1,3] <- ifelse(het_voted_past[1,4]<0.1,paste0(het_voted_past[1,2],"*"),het_voted_past[1,2])
COR_het_voted_past[1,3] <- ifelse(het_voted_past[1,4]<0.05,paste0(het_voted_past[1,2],"**"),COR_het_voted_past[1,3])
COR_het_voted_past[1,3] <- ifelse(het_voted_past[1,4]<0.01,paste0(het_voted_past[1,2],"***"),COR_het_voted_past[1,3])

COR_het_voted_past[3,3] <- ifelse(het_voted_past[2,4]<0.1,paste0(het_voted_past[2,2],"*"),het_voted_past[2,2])
COR_het_voted_past[3,3] <- ifelse(het_voted_past[2,4]<0.05,paste0(het_voted_past[2,2],"**"),COR_het_voted_past[3,3])
COR_het_voted_past[3,3] <- ifelse(het_voted_past[2,4]<0.01,paste0(het_voted_past[2,2],"***"),COR_het_voted_past[3,3])

COR_het_voted_past[5,3] <- ifelse(het_voted_past[3,4]<0.1,paste0(het_voted_past[3,2],"*"),het_voted_past[3,2])
COR_het_voted_past[5,3] <- ifelse(het_voted_past[3,4]<0.05,paste0(het_voted_past[3,2],"**"),COR_het_voted_past[5,3])
COR_het_voted_past[5,3] <- ifelse(het_voted_past[3,4]<0.01,paste0(het_voted_past[3,2],"***"),COR_het_voted_past[5,3])

COR_het_voted_past[7,3] <- ifelse(het_voted_past[4,4]<0.1,paste0(het_voted_past[4,2],"*"),het_voted_past[4,2])
COR_het_voted_past[7,3] <- ifelse(het_voted_past[4,4]<0.05,paste0(het_voted_past[4,2],"**"),COR_het_voted_past[7,3])
COR_het_voted_past[7,3] <- ifelse(het_voted_past[4,4]<0.01,paste0(het_voted_past[4,2],"***"),COR_het_voted_past[7,3])

COR_het_voted_past[9,3] <- ifelse(het_voted_past[5,4]<0.1,paste0(het_voted_past[5,2],"*"),het_voted_past[5,2])
COR_het_voted_past[9,3] <- ifelse(het_voted_past[5,4]<0.05,paste0(het_voted_past[5,2],"**"),COR_het_voted_past[9,3])
COR_het_voted_past[9,3] <- ifelse(het_voted_past[5,4]<0.01,paste0(het_voted_past[5,2],"***"),COR_het_voted_past[9,3])

COR_het_voted_past[11,3] <- ifelse(het_voted_past[6,4]<0.1,paste0(het_voted_past[6,2],"*"),het_voted_past[6,2])
COR_het_voted_past[11,3] <- ifelse(het_voted_past[6,4]<0.05,paste0(het_voted_past[6,2],"**"),COR_het_voted_past[11,3])
COR_het_voted_past[11,3] <- ifelse(het_voted_past[6,4]<0.01,paste0(het_voted_past[6,2],"***"),COR_het_voted_past[11,3])

COR_het_voted_past[2,3] <- paste0("(",het_voted_past[1,3],")")
COR_het_voted_past[4,3] <- paste0("(",het_voted_past[2,3],")")
COR_het_voted_past[6,3] <- paste0("(",het_voted_past[3,3],")")
COR_het_voted_past[8,3] <- paste0("(",het_voted_past[4,3],")")
COR_het_voted_past[10,3] <- paste0("(",het_voted_past[5,3],")")
COR_het_voted_past[12,3] <- paste0("(",het_voted_past[6,3],")")

het_interact_voted_past[,2] <- as.numeric(as.character(het_interact_voted_past[,2]))
het_interact_voted_past[,3] <- as.numeric(as.character(het_interact_voted_past[,3]))
het_interact_voted_past[,4] <- as.numeric(as.character(het_interact_voted_past[,4]))

COR_het_voted_past[13,4] <- "Interaction Term"
COR_het_voted_past[1,4] <- ifelse(het_interact_voted_past[1,4]<0.1,paste0(het_interact_voted_past[1,2],"*"),het_interact_voted_past[1,2])
COR_het_voted_past[1,4] <- ifelse(het_interact_voted_past[1,4]<0.05,paste0(het_interact_voted_past[1,2],"**"),COR_het_voted_past[1,4])
COR_het_voted_past[1,4] <- ifelse(het_interact_voted_past[1,4]<0.01,paste0(het_interact_voted_past[1,2],"***"),COR_het_voted_past[1,4])

COR_het_voted_past[3,4] <- ifelse(het_interact_voted_past[2,4]<0.1,paste0(het_interact_voted_past[2,2],"*"),het_interact_voted_past[2,2])
COR_het_voted_past[3,4] <- ifelse(het_interact_voted_past[2,4]<0.05,paste0(het_interact_voted_past[2,2],"**"),COR_het_voted_past[3,4])
COR_het_voted_past[3,4] <- ifelse(het_interact_voted_past[2,4]<0.01,paste0(het_interact_voted_past[2,2],"***"),COR_het_voted_past[3,4])

COR_het_voted_past[5,4] <- ifelse(het_interact_voted_past[3,4]<0.1,paste0(het_interact_voted_past[3,2],"*"),het_interact_voted_past[3,2])
COR_het_voted_past[5,4] <- ifelse(het_interact_voted_past[3,4]<0.05,paste0(het_interact_voted_past[3,2],"**"),COR_het_voted_past[5,4])
COR_het_voted_past[5,4] <- ifelse(het_interact_voted_past[3,4]<0.01,paste0(het_interact_voted_past[3,2],"***"),COR_het_voted_past[5,4])

COR_het_voted_past[7,4] <- ifelse(het_interact_voted_past[4,4]<0.1,paste0(het_interact_voted_past[4,2],"*"),het_interact_voted_past[4,2])
COR_het_voted_past[7,4] <- ifelse(het_interact_voted_past[4,4]<0.05,paste0(het_interact_voted_past[4,2],"**"),COR_het_voted_past[7,4])
COR_het_voted_past[7,4] <- ifelse(het_interact_voted_past[4,4]<0.01,paste0(het_interact_voted_past[4,2],"***"),COR_het_voted_past[7,4])

COR_het_voted_past[9,4] <- ifelse(het_interact_voted_past[5,4]<0.1,paste0(het_interact_voted_past[5,2],"*"),het_interact_voted_past[5,2])
COR_het_voted_past[9,4] <- ifelse(het_interact_voted_past[5,4]<0.05,paste0(het_interact_voted_past[5,2],"**"),COR_het_voted_past[9,4])
COR_het_voted_past[9,4] <- ifelse(het_interact_voted_past[5,4]<0.01,paste0(het_interact_voted_past[5,2],"***"),COR_het_voted_past[9,4])

COR_het_voted_past[11,4] <- ifelse(het_interact_voted_past[6,4]<0.1,paste0(het_interact_voted_past[6,2],"*"),het_interact_voted_past[6,2])
COR_het_voted_past[11,4] <- ifelse(het_interact_voted_past[6,4]<0.05,paste0(het_interact_voted_past[6,2],"**"),COR_het_voted_past[11,4])
COR_het_voted_past[11,4] <- ifelse(het_interact_voted_past[6,4]<0.01,paste0(het_interact_voted_past[6,2],"***"),COR_het_voted_past[11,4])

COR_het_voted_past[2,4] <- paste0("(",het_interact_voted_past[1,3],")")
COR_het_voted_past[4,4] <- paste0("(",het_interact_voted_past[2,3],")")
COR_het_voted_past[6,4] <- paste0("(",het_interact_voted_past[3,3],")")
COR_het_voted_past[8,4] <- paste0("(",het_interact_voted_past[4,3],")")
COR_het_voted_past[10,4] <- paste0("(",het_interact_voted_past[5,3],")")
COR_het_voted_past[12,4] <- paste0("(",het_interact_voted_past[6,3],")")

COR_het_voted_past

#Table A19:
stargazer(as.matrix(COR_het_voted_past), rownames=F, out="TableA19.tex")


###Sensitivity analysis: Dropping one province at a time

COR_sensitivity_province <- data.frame(matrix(NA, nrow=(length(unique(COR$reg))*6), ncol=6))
colnames(COR_sensitivity_province) <- c("ProvinceDropped","Variable","Effect","SE","pValue","N")

for (i in 1:length(unique(COR$reg))){
  COR_temp <- subset(COR, COR$reg!=unique(COR$reg)[i])
  
  outcomes_temp <- rbind(
    c("progcorrupt",
      summary(lm(progcorrupt ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR_temp))$coefficients[1,1],
      summary(lm(progcorrupt ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR_temp))$coefficients[2,][c(1,2,4)],
      sum(ifelse(!is.na(subset(COR_temp,COR_temp$treat14==0|COR_temp$treat14==1)$progcorrupt),1,0))),
    c("participation_willingness",
      summary(lm(participation_willingness ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR_temp))$coefficients[1,1],
      summary(lm(participation_willingness ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR_temp))$coefficients[2,][c(1,2,4)],
      sum(ifelse(!is.na(subset(COR_temp,COR_temp$treat14==0|COR_temp$treat14==1)$participation_willingness),1,0))),
    c("vote_invalid",
      summary(lm(vote_invalid ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR_temp))$coefficients[1,1],
      summary(lm(vote_invalid ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR_temp))$coefficients[2,][c(1,2,4)],
      sum(ifelse(!is.na(subset(COR_temp,COR_temp$treat14==0|COR_temp$treat14==1)$vote_invalid),1,0))),
    c("conf_judge",
      summary(lm(conf_judge ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR_temp))$coefficients[1,1],
      summary(lm(conf_judge ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR_temp))$coefficients[2,][c(1,2,4)],
      sum(ifelse(!is.na(subset(COR_temp,COR_temp$treat14==0|COR_temp$treat14==1)$conf_judge),1,0))),
    c("conf_parties",
      summary(lm(conf_parties ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR_temp))$coefficients[1,1],
      summary(lm(conf_parties ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR_temp))$coefficients[2,][c(1,2,4)],
      sum(ifelse(!is.na(subset(COR_temp,COR_temp$treat14==0|COR_temp$treat14==1)$conf_parties),1,0))),
    c("conf_parl",
      summary(lm(conf_parl ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR_temp))$coefficients[1,1],
      summary(lm(conf_parl ~ treat14 + male + age + complete_highschool + university + laborforce + poverty + voted + factor(ciudad),data=COR_temp))$coefficients[2,][c(1,2,4)],
      sum(ifelse(!is.na(subset(COR_temp,COR_temp$treat14==0|COR_temp$treat14==1)$conf_parl),1,0)))
  )
  
  table_sens <- outcomes_temp[,c(1,3:6)]
  table_sens <- as.matrix(table_sens)
  
  table_sens[,1] <- c("Progress on corruption","Inclination to vote or protest","Invalid vote","Trust in judiciary","Trust in parties","Trust in congress")
  
  table_sens <- cbind(rep(unique(as.character(COR$reg))[i],6),table_sens)
  
  COR_sensitivity_province[c((i*6-5):(i*6)),] <- table_sens
}

COR_sensitivity_province

COR_sensitivity_province$Effect <- as.numeric(COR_sensitivity_province$Effect)
COR_sensitivity_province$SE <- as.numeric(COR_sensitivity_province$SE)
COR_sensitivity_province$pValue <- as.numeric(COR_sensitivity_province$pValue)

COR_sensitivity_province$ProvinceDropped[COR_sensitivity_province$ProvinceDropped=="CR: San José"] <- "San José"
COR_sensitivity_province$ProvinceDropped[COR_sensitivity_province$ProvinceDropped=="CR: Alajuela"] <- "Alajuela"
COR_sensitivity_province$ProvinceDropped[COR_sensitivity_province$ProvinceDropped=="CR: Cartago"] <- "Cartago"
COR_sensitivity_province$ProvinceDropped[COR_sensitivity_province$ProvinceDropped=="CR: Heredia"] <- "Heredia"
COR_sensitivity_province$ProvinceDropped[COR_sensitivity_province$ProvinceDropped=="CR: Guanacaste"] <- "Guanacaste"
COR_sensitivity_province$ProvinceDropped[COR_sensitivity_province$ProvinceDropped=="CR: Puntarenas"] <- "Puntarenas"
COR_sensitivity_province$ProvinceDropped[COR_sensitivity_province$ProvinceDropped=="CR: Limón"] <- "Limón"

q1 <- ggplot(subset(COR_sensitivity_province,Variable=="Progress on corruption"), aes(y=Effect,x=ProvinceDropped)) +
  ggtitle("Progress on corruption")+
  theme(plot.title=element_text(size=16, face="bold", hjust=0.5))+
  theme(plot.margin=unit(c(0.5,0.5,0.5,0.5),"cm"))+
  geom_hline(yintercept=0, linetype="dotted",size=0.9)+
  ylab("Treatment effect")+
  xlab("Region dropped")+
  theme(axis.text.x = element_text(angle = 0,vjust=0.5,hjust=0.5))+
  scale_color_manual(values=c("darkblue", "red"))+
  theme(text=element_text(size=16))+
  theme(axis.text.y=element_text(size=16, color="black"))+
  theme(axis.text.x=element_text(size=16, color="black"))+
  theme(legend.position = "none",legend.title = element_blank())+
  scale_y_continuous(breaks=c(-1.25,-1,-0.75,-0.5,-0.25,0,0.25,0.5,0.75,1,1.25), limits=c(-1.25,1.25))+
  geom_point(size=4,position = position_dodge(.9))+
  geom_errorbar(aes(ymax=Effect + (SE*-qnorm((1-0.90)/2)), ymin=Effect - (SE*-qnorm((1-0.90)/2))), width=.4, position=position_dodge(.9),stat="identity",size=0.8)+ 
  geom_errorbar(aes(ymax=Effect + (SE*(-qnorm((1-0.95)/2))), ymin=Effect - (SE*(-qnorm((1-0.95)/2)))), width=.0, position=position_dodge(.9),stat="identity",size=0.8)+
  coord_flip()

q2 <- ggplot(subset(COR_sensitivity_province,Variable=="Inclination to vote or protest"), aes(y=Effect,x=ProvinceDropped)) +
  ggtitle("Inclination to vote or protest")+
  theme(plot.title=element_text(size=16, face="bold", hjust=0.5))+
  theme(plot.margin=unit(c(0.5,0.5,0.5,0.5),"cm"))+
  geom_hline(yintercept=0, linetype="dotted",size=0.9)+
  ylab("Treatment effect")+
  xlab("Region dropped")+
  theme(axis.text.x = element_text(angle = 0,vjust=0.5,hjust=0.5))+
  scale_color_manual(values=c("darkblue", "red"))+
  theme(text=element_text(size=16))+
  theme(axis.text.y=element_text(size=16, color="black"))+
  theme(axis.text.x=element_text(size=16, color="black"))+
  theme(legend.position = "none",legend.title = element_blank())+
  scale_y_continuous(breaks=c(-1.25,-1,-0.75,-0.5,-0.25,0,0.25,0.5,0.75,1,1.25), limits=c(-1.25,1.25))+
  geom_point(size=4,position = position_dodge(.9))+
  geom_errorbar(aes(ymax=Effect + (SE*-qnorm((1-0.90)/2)), ymin=Effect - (SE*-qnorm((1-0.90)/2))), width=.4, position=position_dodge(.9),stat="identity",size=0.8)+ 
  geom_errorbar(aes(ymax=Effect + (SE*(-qnorm((1-0.95)/2))), ymin=Effect - (SE*(-qnorm((1-0.95)/2)))), width=.0, position=position_dodge(.9),stat="identity",size=0.8)+
  coord_flip()

q3 <- ggplot(subset(COR_sensitivity_province,Variable=="Invalid vote"), aes(y=Effect,x=ProvinceDropped)) +
  ggtitle("Invalid vote")+
  theme(plot.title=element_text(size=16, face="bold", hjust=0.5))+
  theme(plot.margin=unit(c(0.5,0.5,0.5,0.5),"cm"))+
  geom_hline(yintercept=0, linetype="dotted",size=0.9)+
  ylab("Treatment effect")+
  xlab("Region dropped")+
  theme(axis.text.x = element_text(angle = 0,vjust=0.5,hjust=0.5))+
  scale_color_manual(values=c("darkblue", "red"))+
  theme(text=element_text(size=16))+
  theme(axis.text.y=element_text(size=16, color="black"))+
  theme(axis.text.x=element_text(size=16, color="black"))+
  theme(legend.position = "none",legend.title = element_blank())+
  scale_y_continuous(breaks=c(-1.25,-1,-0.75,-0.5,-0.25,0,0.25,0.5,0.75,1,1.25), limits=c(-1.25,1.25))+
  geom_point(size=4,position = position_dodge(.9))+
  geom_errorbar(aes(ymax=Effect + (SE*-qnorm((1-0.90)/2)), ymin=Effect - (SE*-qnorm((1-0.90)/2))), width=.4, position=position_dodge(.9),stat="identity",size=0.8)+ 
  geom_errorbar(aes(ymax=Effect + (SE*(-qnorm((1-0.95)/2))), ymin=Effect - (SE*(-qnorm((1-0.95)/2)))), width=.0, position=position_dodge(.9),stat="identity",size=0.8)+
  coord_flip()

q4 <- ggplot(subset(COR_sensitivity_province,Variable=="Trust in judiciary"), aes(y=Effect,x=ProvinceDropped)) +
  ggtitle("Trust in judiciary")+
  theme(plot.title=element_text(size=16, face="bold", hjust=0.5))+
  theme(plot.margin=unit(c(0.5,0.5,0.5,0.5),"cm"))+
  geom_hline(yintercept=0, linetype="dotted",size=0.9)+
  ylab("Treatment effect")+
  xlab("Region dropped")+
  theme(axis.text.x = element_text(angle = 0,vjust=0.5,hjust=0.5))+
  scale_color_manual(values=c("darkblue", "red"))+
  theme(text=element_text(size=16))+
  theme(axis.text.y=element_text(size=16, color="black"))+
  theme(axis.text.x=element_text(size=16, color="black"))+
  theme(legend.position = "none",legend.title = element_blank())+
  scale_y_continuous(breaks=c(-1.25,-1,-0.75,-0.5,-0.25,0,0.25,0.5,0.75,1,1.25), limits=c(-1.25,1.25))+
  geom_point(size=4,position = position_dodge(.9))+
  geom_errorbar(aes(ymax=Effect + (SE*-qnorm((1-0.90)/2)), ymin=Effect - (SE*-qnorm((1-0.90)/2))), width=.4, position=position_dodge(.9),stat="identity",size=0.8)+ 
  geom_errorbar(aes(ymax=Effect + (SE*(-qnorm((1-0.95)/2))), ymin=Effect - (SE*(-qnorm((1-0.95)/2)))), width=.0, position=position_dodge(.9),stat="identity",size=0.8)+
  coord_flip()

q5 <- ggplot(subset(COR_sensitivity_province,Variable=="Trust in parties"), aes(y=Effect,x=ProvinceDropped)) +
  ggtitle("Trust in parties")+
  theme(plot.title=element_text(size=16, face="bold", hjust=0.5))+
  theme(plot.margin=unit(c(0.5,0.5,0.5,0.5),"cm"))+
  geom_hline(yintercept=0, linetype="dotted",size=0.9)+
  ylab("Treatment effect")+
  xlab("Region dropped")+
  theme(axis.text.x = element_text(angle = 0,vjust=0.5,hjust=0.5))+
  scale_color_manual(values=c("darkblue", "red"))+
  theme(text=element_text(size=16))+
  theme(axis.text.y=element_text(size=16, color="black"))+
  theme(axis.text.x=element_text(size=16, color="black"))+
  theme(legend.position = "none",legend.title = element_blank())+
  scale_y_continuous(breaks=c(-1.25,-1,-0.75,-0.5,-0.25,0,0.25,0.5,0.75,1,1.25), limits=c(-1.25,1.25))+
  geom_point(size=4,position = position_dodge(.9))+
  geom_errorbar(aes(ymax=Effect + (SE*-qnorm((1-0.90)/2)), ymin=Effect - (SE*-qnorm((1-0.90)/2))), width=.4, position=position_dodge(.9),stat="identity",size=0.8)+ 
  geom_errorbar(aes(ymax=Effect + (SE*(-qnorm((1-0.95)/2))), ymin=Effect - (SE*(-qnorm((1-0.95)/2)))), width=.0, position=position_dodge(.9),stat="identity",size=0.8)+
  coord_flip()

q6 <- ggplot(subset(COR_sensitivity_province,Variable=="Trust in congress"), aes(y=Effect,x=ProvinceDropped)) +
  ggtitle("Trust in congress")+
  theme(plot.title=element_text(size=16, face="bold", hjust=0.5))+
  theme(plot.margin=unit(c(0.5,0.5,0.5,0.5),"cm"))+
  geom_hline(yintercept=0, linetype="dotted",size=0.9)+
  ylab("Treatment effect")+
  xlab("Region dropped")+
  theme(axis.text.x = element_text(angle = 0,vjust=0.5,hjust=0.5))+
  scale_color_manual(values=c("darkblue", "red"))+
  theme(text=element_text(size=16))+
  theme(axis.text.y=element_text(size=16, color="black"))+
  theme(axis.text.x=element_text(size=16, color="black"))+
  theme(legend.position = "none",legend.title = element_blank())+
  scale_y_continuous(breaks=c(-1.25,-1,-0.75,-0.5,-0.25,0,0.25,0.5,0.75,1,1.25), limits=c(-1.25,1.25))+
  geom_point(size=4,position = position_dodge(.9))+
  geom_errorbar(aes(ymax=Effect + (SE*-qnorm((1-0.90)/2)), ymin=Effect - (SE*-qnorm((1-0.90)/2))), width=.4, position=position_dodge(.9),stat="identity",size=0.8)+ 
  geom_errorbar(aes(ymax=Effect + (SE*(-qnorm((1-0.95)/2))), ymin=Effect - (SE*(-qnorm((1-0.95)/2)))), width=.0, position=position_dodge(.9),stat="identity",size=0.8)+
  coord_flip()

COR_sensitivity_graph <- grid.arrange(q1, q2, q3, q4, q5, q6, ncol = 2)
ggsave("FigureA6.pdf", plot = COR_sensitivity_graph, width = 18, height = 15)

